storing variables

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

storing variables

#1 Post by giorgino » 16 May 2023, 00:01

I'm building a simple level counter of a tank, using a waterflow transducer.
It works good, and the data is correctly published on my HA server via MQTT.

I can't find a solution to save the data during a power off of the ESP32
The solution should be (in rule):

Code: Select all

ON [WATERFLOW#count] do
 let,1,[WATERFLOW#count]
endon
and write VAR1 in a nonvolatile site, then ON REBOOT update VAR1 with stored data.

...but I've no idea about how to solve this
Please help me, thanks
Gio

Micha_he
Normal user
Posts: 369
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: storing variables

#2 Post by Micha_he » 16 May 2023, 07:51

You can use a Dummy-Device, where the variables survive a reboot.
Shall the var's survive a power-out, then you would have to store the values externally using MQTT, for example.

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: storing variables

#3 Post by TD-er » 16 May 2023, 08:25

You could look into publishing to a topic using the retain flag and then let MQTT import subscribe to that topic.
Never tried it, but I think it might just work, since the retain flag means that anyone subscribing to such a topic will get the last message sent to it, when it subscribes.

See: https://www.hivemq.com/blog/mqtt-essent ... -messages/
Not sure if it also works when you reconnect using the same client ID

Currently you can only set the retain flag in the MQTT controller, but I think we should also add this as an option for the publish command as it can be a really useful feature to have.

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#4 Post by giorgino » 16 May 2023, 18:46

Micha_he wrote: 16 May 2023, 07:51 You can use a Dummy-Device, where the variables survive a reboot.
Shall the var's survive a power-out, then you would have to store the values externally using MQTT, for example.
Do you know how long it survives? My application is for the level in the water tank on a boat, so battery sometimes stay off for long time (months ?)
TD-er wrote: 16 May 2023, 08:25 You could look into publishing to a topic using the retain flag and then let MQTT import subscribe to that topic.
Never tried it, but I think it might just work, since the retain flag means that anyone subscribing to such a topic will get the last message sent to it, when it subscribes.

See: https://www.hivemq.com/blog/mqtt-essent ... -messages/
Not sure if it also works when you reconnect using the same client ID

Currently you can only set the retain flag in the MQTT controller, but I think we should also add this as an option for the publish command as it can be a really useful feature to have.
This solution works good, now the system publish the data on a dashboard online via MQTT and on a local display, but sometimes the MQTT is unreacheable, so I think the data should be lost

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: storing variables

#5 Post by TD-er » 16 May 2023, 19:00

You can also use the "Level" plugin, which does actually store the set level in the settings.
Just make sure not to call this too often as it will wear out the flash and it can also cause file system fragmentation on small file systems (typically 1M flash units can experience issues with fragmentation)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#6 Post by giorgino » 16 May 2023, 19:21

TD-er wrote: 16 May 2023, 19:00 You can also use the "Level" plugin, which does actually store the set level in the settings.
Just make sure not to call this too often as it will wear out the flash and it can also cause file system fragmentation on small file systems (typically 1M flash units can experience issues with fragmentation)
this sounds very good! I go to study the plugins!
No problem for the frequent calls, it will store only every change in variable, 10 times a day, (in days of use)

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#7 Post by Ath » 16 May 2023, 21:05

That plugin by default does not save the data to storage, it needs a save command for that, but you can configure a time-out, like 30 or 60 minutes, after which the settings will be saved *only* if the value has changed since the last save. That's designed that way to try and lower the number of saves, trying to protect the flash from wearing out (fast).
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#8 Post by giorgino » 17 May 2023, 19:23

something goes wrong in my task.....
I count with flowmeter and pass the data in %var1%, than the
"Device: Regulator - Level Control Name: WaterLevel"
check the TASK and get the value from
"GENERIC- PULSE COUNTER"
but the data acquired and stored in "output" is different from %var1%

Where do I am wrong ?

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#9 Post by Ath » 17 May 2023, 19:35

How do you set the value of the Level task?
Please share your rules (by using a code block, the </> button in the forum toolbar)
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#10 Post by giorgino » 17 May 2023, 23:31

How do you set the value of the Level task?
ok, I'm wrong....

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#11 Post by giorgino » 18 May 2023, 07:46

Code: Select all

config,task,DimmiWaterLevel,SetLevel,180

On [waterflow#TOTALE_dal_reset] Do
    Let,1,180-[waterflow#TOTALE_dal_reset]  //quando sente un aggiornamento di  [waterflow#TOTAL_dal_reset] lo scrive in var1
   
Endon
 

Code: Select all

Device:	Regulator - Level Control ? i
Name:	DimmiWaterLevel
Enabled:	Sensor
GPIO → Level low:	- None -
Device Settings
Check Task:	6 - Generic - Pulse counter - waterflow
Check Value:	TOTALE_dal_reset
Set Level:	0.00
Hysteresis:	0.00
Save 'Set Level' after change via config command:	
Auto-save interval:	1  [minutes]

Send to Controller ❶
(Home Assistant (openHAB) MQTT)
Values
#	Name	
1	Output


and the data comes from this:

Code: Select all

Device:	Generic - Pulse counter ? i
Name:	waterflow
Enabled:	Sensor
Internal PullUp:	
GPIO ← Pulse:	GPIO-27
Device Settings Debounce Time (mSec):	20
Counter Type:	Delta
Mode Type:	Change
Data Acquisition Single event with all values:	
Send to Controller ❶
(Home Assistant (openHAB) MQTT)
Interval:	6 [sec]
Values
#	Name	Formula ?	Stats	Decimals
1	Count 				0 
2	TOTALE_dal_reset 		0
3	none
and the only output I get is: (in MQTT)


TOTALE_dal_reset = 375

DimmiWaterLevel
Output = 0

and, in the LOG:
9245: Skip saving SecuritySettings, not changed
46079873: EVENT: TaskExit#DimmiWaterLevel=8,0
46080049: static_file: /esp.css
46080122: EVENT: TaskInit#DimmiWaterLevel=8,1
46082228: EVENT: waterflow#Count=0
46100233: EVENT: waterflow#TOTALE_dal_reset=375
46082247

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#12 Post by Ath » 18 May 2023, 11:40

giorgino wrote: 18 May 2023, 07:46

Code: Select all

config,task,DimmiWaterLevel,SetLevel,180

On [waterflow#TOTALE_dal_reset] Do
    Let,1,180-[waterflow#TOTALE_dal_reset]  //quando sente un aggiornamento di  [waterflow#TOTAL_dal_reset] lo scrive in var1
   
Endon
 
There are a few problems in your code:

- A 'loose' statement, not in an event handler, is never executed, so the config,task... can be removed, as you can set the initial value in the Task itself, and on restart or whatever event you want the last stored value, not reset it to some default.

- An event should not have square brackets around the name, as then the rules processor will replace that by the value of the variable, a correct way to write that handler could be:

Code: Select all

On waterflow#TOTALE_dal_reset Do
    Let,1,180-%eventvalue1%  //quando sente un aggiornamento di  [waterflow#TOTAL_dal_reset] lo scrive in var1 (using the argument provided with the event)
    Config,task,DimmiWaterLevel,SetLevel,[var#1] // Store new level in the level task, that is configured to store it automatically
Endon
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#13 Post by giorgino » 18 May 2023, 12:29

thank you very much!

As you know, my tank max contains 180 litres, so I subtract the consuption from 180, to obtain the level by the difference.

In your code I don't understand where it get the updated value from the memory (or I have to set it in the device settings?)


I'm really a beginner....

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#14 Post by Ath » 18 May 2023, 13:13

Hm, the entire requirement of storing the value during a power-loss period seems not very relevant, as the first thing after a (cold) boot would be that the level is measured, and sent out to HA. That will be the current level. Why would that value need to be stored for later use?

How are you currently sending the level to HA? Using a publish command in rules, or by configuring some task to use the HA controller, as configured on the Controllers tab? And when the last situation is used, how is the measured value set in the task?
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#15 Post by giorgino » 18 May 2023, 17:39

The water level is measured only counting the flow in exit from the tank, with decremental count from the full level (180 lt), recording this number in a variable that reset itself in case of power loss.
There is five days of power loss every week (battery on boat are disconnected weekly) so the level need to be stored during the updating and retrieved at the next power on.

The level is sended to HA via MQTT (over internet), but the connection often is down, so at the power on I'm not sure about the internet connection to retrieve the data from the MQTT broker installed near HA.
For this reason I was looking for a solution of storing the data in local ESP32 eeprom

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#16 Post by Ath » 18 May 2023, 19:09

Ok, now it's clear to me. I'll be back later
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#17 Post by Ath » 18 May 2023, 21:07

Something like this:

Code: Select all

On waterflow#Count Do // !! Pulse-count since last event !!
  Let,1,[DimmiWaterLevel#GetLevel]-%eventvalue1%  //quando sente un aggiornamento di  [waterflow#TOTAL_dal_reset] lo scrive in var1 (using the argument provided with the event)
  Config,task,DimmiWaterLevel,SetLevel,[var#1] // Store new level in the level task, that is configured to store it automatically
Endon

// Extra feature: Reset switch
On ResetSwitch#State=0 Do // Respond to a switch task named ResetSwitch that when pressed connects a GPIO pin to GND. Be sure to set a De-bounce value (250 msec is fine, and the max. allowed) and use a pull-up resistor of 4k7 or 10k.
  Config,task,DimmiWaterLevel,SetLevel,180 // Reset the level to its 'full' state. Will be auto-saved
Endon
Last edited by Ath on 18 May 2023, 22:50, edited 1 time in total.
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#18 Post by giorgino » 18 May 2023, 22:01

that's great! but:

1: for the reset switch I was using the RESETCOUNTER internal function. Isn't it good?


2: are you sure about dimmiwaterlevel#output in Let,1,[DimmiWaterLevel#Output]-%eventvalue1% ??

that output is always 0 and switch to 1 only when something changes in DimmiWaterLevel


3: after a powerloss it is returning to 0 :(

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#19 Post by Ath » 18 May 2023, 22:49

Ah, yes, that should be getlevel, I'll update that.
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#20 Post by giorgino » 18 May 2023, 22:58

ok, all right,

BUT after powerloss I can't recover old data, they are set to zero. Why??

:cry:

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: storing variables

#21 Post by Ath » 18 May 2023, 23:03

Did you set the Auto-save interval in the Level task, f.e. 5 minutes, and wait at least that time before pulling the power?
/Ton (PayPal.me)

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#22 Post by giorgino » 18 May 2023, 23:16

yes, just to try i put it at 2 minutes
In the log I find this:
31: ACT : config,task,DimmiWaterLevel,SetLevel,23
571038: SaveToFile: free stack: 6024
571140: FILE : Saved config.dat offset: 0 size: 3020
571140: SaveToFile: free stack after: 6024
571141: Skip saving SecuritySettings, not changed
571166: ACT : Publish,'WaterLevel',23
But in rules do I need to set something about "time#Initialized" or so? (for recover the data)

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: storing variables

#23 Post by TD-er » 18 May 2023, 23:22

See:

https://espeasy.readthedocs.io/en/lates ... -available

The stored value can be 'read' via [<taskname>#GetLevel]

So on the system#boot event you can store this value in a variable:

Code: Select all

on System#Boot do
  let,1,[<taskname>#GetLevel]
endon
Don't forget to change with your taskname and variable nr.

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#24 Post by giorgino » 19 May 2023, 01:04

everythings is working!!!
Thank you very much 8-) 8-) 8-)

A last question: for the reset switch, that works closing to gnd, isn't enough the internal pullup resistor? do I need the external pullup resistor?

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: storing variables

#25 Post by TD-er » 19 May 2023, 01:14

You don't want to have it trigger on just some noise.
By having some resistor, you increase the amount of current needed to toggle the logic level.

giorgino
Normal user
Posts: 63
Joined: 24 Oct 2022, 21:06

Re: storing variables

#26 Post by giorgino » 19 May 2023, 02:09

ok, perfect

bye

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests