Persistent Variable

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Persistent Variable

#1 Post by wiredcharlie » 22 Aug 2022, 09:39

Hi
I've made an Espresso temperature controller and need to be able to store the set temperature as a variable that survives power loss. My plan is to use a custom dashboard.esp to set the temperature with a simple web interface.

I can't see a way of storing a persistent variable.

Please help!

Tony

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

Re: Persistent Variable

#2 Post by Ath » 22 Aug 2022, 10:12

There is no official way of storing a setting, other than having that in a Config page for a plugin, but there is an accepted work-around: You can use the 'Regulator - Level control' (P021) to set a value using the 'setlevel,(value)' command. It has an option to save the value immediately (not recommended) or you can save all settings using a 'save' command at a convenient time (should be used sparingly, to avoid damaging the Flash memory, as the max number of flash-writes are limited).

Some safety is already built into the plugin so it won't save if the value hasn't changed, but avoiding automatically saving that value every time it changes is strongly advised. You can set a flag once the value is changed, and check that flag, say once per 30 minutes, and only save if really needed.

Hm, that's actually a nice feature request, adding a configurable "auto-save after x minutes when changed" option :idea:
I'll put that on my TODO list ;)
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#3 Post by wiredcharlie » 22 Aug 2022, 12:07

Thanks for that. What would the URL look like to set the Set Level?

I tried /control?cmd=config,3,Set_Temp,SetLevel,89

...but I seem to be barking up the wrong tree.

Help appreciated.

Tony

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

Re: Persistent Variable

#4 Post by Ath » 22 Aug 2022, 13:08

That url looks like you combined 2 commands, but setlevel is a separate command, specific to the Level plugin.

NB: When testing a command from the Tools page of ESPEasy, you can see the url in your browsers address bar
/Ton (PayPal.me)

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

Re: Persistent Variable

#5 Post by Ath » 22 Aug 2022, 20:26

It was me that was mixed up, sorry, I mistakenly assumed the plugin used a regular command, but it needs to use the config command like: 'config,task,(taskname),setlevel,(value)',
so for your example, it should look like:

Code: Select all

config,task,Set_Temp,SetLevel,89
The second argument should literally be 'task', as the original intention was that using the 'config' command, all type of settings, like Tasks, Controllers, Notifications, etc., could be changed, but that hasn't been continued (yet).
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#6 Post by wiredcharlie » 23 Aug 2022, 15:44

Thanks, that works. But then in the Rules, [Set_Temp#GetLevel] does not return a value.

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

Re: Persistent Variable

#7 Post by TD-er » 23 Aug 2022, 16:06

How do you use it in the rules?

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

I assume your taskname is Set_temp ?

This should even work when called from command field on the tools page.

Code: Select all

logEntry,"The level is: [Set_temp #GetLevel]"

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#8 Post by wiredcharlie » 23 Aug 2022, 17:10

That's what I thought, but no result.

From rules:

On Rules#Timer=1 do

// Target temp

let,1,[Set_Temp#GetLevel]

Log:

4509072: EVENT: Rules#Timer=1,1
4509095: ACT : let,1,Y
4509100: Calculate: Unknown token input: Y = 0

4509106: ACT : let,2,0-33
4509115: ACT : let,3,0-15
4509124: ACT : let,4,0-7
4509133: ACT : let,5,0-2
4509141: ACT : let,6,0-1

Sorry about this, I'm probably doing something silly!

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

Re: Persistent Variable

#9 Post by Ath » 23 Aug 2022, 17:15

To be explicit, your rule should at least contain these lines:

Code: Select all

on Rules#Timer=1 do
  Let,1,[Set_Temp#GetLevel]
endon
Edit: Fixed a typo
Last edited by Ath on 23 Aug 2022, 19:53, edited 1 time in total.
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#10 Post by wiredcharlie » 23 Aug 2022, 18:45

I notice you added a comma - but that didn't help. endon is included - puzzled now!

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

Re: Persistent Variable

#11 Post by TD-er » 23 Aug 2022, 19:11

You forgot the "endon" indeed.
The comma is a typo from Ton.

Code: Select all

on Rules#Timer=1 do
  Let,1,[Set_Temp#GetLevel]
endon

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#12 Post by wiredcharlie » 23 Aug 2022, 19:15

No I didn't forget the endon. :D I just didn't post the full script:

Code: Select all

On System#Boot do  
  GPIO,0,0
  
  oledframedcmd,linecount,2
  oledframedcmd,display,high
  oledframedcmd,1,Target:
  oledframedcmd,2,[Set_Temp#GetLevel]
  delay,2000
  timerSet,1,2 
endon




On Rules#Timer=1 do 

  //     Target temp

         let,1,[Set_Temp,#GetLevel]




let,2,[VAR#1]-33
let,3,[VAR#1]-15
let,4,[VAR#1]-7
let,5,[VAR#1]-2
let,6,[VAR#1]-1
let,12,[VAR#1]+2



    let,10,[TEMP#Temperature]
    oledframedcmd,1,[INT#10]°c
    oledframedcmd,2,

if [Disable#Element]=0 AND [TEMP#Temperature]>0
  



				    if [TEMP#Temperature]<[VAR#2] AND [Pump#State]=0
				        GPIO,0,1
				         oledframedcmd,2,100%
				    else
				        GPIO,0,0

						         if [TEMP#Temperature]>[VAR#2] AND [TEMP#Temperature]<[VAR#4]
						            let,7,([VAR#1]-[TEMP#Temperature])*10
						            PWM,0,[INT#7]
						            let,8,[INT#7]/10
						            oledframedcmd,2,[INT#8]%
						         endif




						         if [TEMP#Temperature]>[VAR#4] AND [TEMP#Temperature]<[VAR#1]
						            
						                      let,7,([VAR#1]-[TEMP#Temperature])*70
						                 

						            PWM,0,[INT#7]
						            let,8,[INT#7]/10
						            oledframedcmd,2,[INT#8]%
						            delay,3000
						            GPIO,0,0
						            oledframedcmd,2,
						               if [TEMP#Temperature]>[VAR#6]
						                   delay,10000
						               else
						                    delay,5000
						               endif
						         endif

						         if [TEMP#Temperature]>[VAR#1]
						              oledframedcmd,2,
						         endif
		            endif
           

else
      GPIO,0,0

endif

		  if [Pump#State]=1 AND [TEMP#Temperature]<[VAR#12]
		      PWM,0,200
	              oledframedcmd,2,'20% Brew!'
	          endif

    timerSet,1,2     


endon



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

Re: Persistent Variable

#13 Post by TD-er » 23 Aug 2022, 20:27

please don't use delay in rules.
That's an absolute big no-no.

There are timers, you can act on events like WiFi#connected etc.

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

Re: Persistent Variable

#14 Post by Ath » 23 Aug 2022, 20:34

Those OledFramed commands can also be set in the configuration for that device, no need to do that from rules.

Restarting the timer at the end isn't needed, you can use a LoopTimerSet command, that repeats that timer indefinitely or a predefined number of times (3rd option). I'd use a bit more than 2 seconds, though, that huge rule will keep the ESP quite busy.

For readability you might want to remove the excess tabs, spaces and blank lines, as currently it isn't very comprehensible.
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#15 Post by wiredcharlie » 24 Aug 2022, 00:47

Thanks for the coding tips - I will optimise it with your suggestions.

But back to the persistent problem - can you see why that won't work?

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

Re: Persistent Variable

#16 Post by Ath » 24 Aug 2022, 09:00

IMHO, my reply #5 is the correct way to store a different value in the Level Control.
And you can either use the 'save' command to store it persistently, or enable the 'Save...' checkbox in the device configuration to have it persisted immediately on change (not recommeended), until PR #4219 is merged that can save it after a set interval once changed (also can be changed multiple times before it is saved, only the first change starts the interval timer, and if the original value is set again no 'save' is applied).
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#17 Post by wiredcharlie » 24 Aug 2022, 11:27

Ath - I'm not disagreeing with you at all - and I'm really grateful for your help.

In fact, I'm so grateful that such a powerful tool (ESP EASY) is available for free. To have made so much progress with this project with so little programming is amazing. Thank you!

I agree that what you have suggested should work - it's just that I can't get it to work on my ESP.

Tony

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

Re: Persistent Variable

#18 Post by Ath » 24 Aug 2022, 11:43

wiredcharlie wrote: 24 Aug 2022, 11:27 .. it's just that I can't get it to work on my ESP.
That's quite easy to confirm,
1) copy the next code: (assuming that your current setting <> 89, else choose a different value than 89)

Code: Select all

config,task,Set_Temp,SetLevel,89
2) paste that in the Command field on the Tools page and click Submit
3) Response (Command Output) should be OK
4) Go to the Devices tab and open the device with name Set_Temp
5) The 'Set Level' should be the value you used in step 1 -> Success! :)

6) Go back to the Tools tab, and enter in the Command field:

Code: Select all

[Set_Temp#GetLevel]
and click Submit

7) The Command Output should be "Command unknown: 89.00" (or the value you entered in step 1) -> Success again! :)
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#19 Post by wiredcharlie » 24 Aug 2022, 12:03

Screenshot 2022-08-24 at 10.57.11.png
Screenshot 2022-08-24 at 10.57.11.png (23.83 KiB) Viewed 4493 times
Build: ESP_Easy_mega_20220809_normal_302_ESP8266_4M1M Aug 9 2022

:cry:

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

Re: Persistent Variable

#20 Post by Ath » 24 Aug 2022, 12:04

What does the "Set Level" part of the Device Configuration page show?
/Ton (PayPal.me)

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

Re: Persistent Variable

#21 Post by Ath » 24 Aug 2022, 12:06

And what exact build of ESPEasy do you have installed (lower-right of most webpages of your ESP)
/Ton (PayPal.me)

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

Re: Persistent Variable

#22 Post by TD-er » 24 Aug 2022, 12:45

Also is the task enabled?

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

Re: Persistent Variable

#23 Post by Ath » 24 Aug 2022, 19:38

wiredcharlie wrote: 24 Aug 2022, 12:03 Build: ESP_Easy_mega_20220809_normal_302_ESP8266_4M1M Aug 9 2022
I'm seeing something peculiar with that build when I compile and run it locally. Can you replace it by the normal normal build: ESP_Easy_mega_20220809_normal_ESP8266_4M1M and try this again?
I also have a fix, but that will on earliest be included in a next release.
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#24 Post by wiredcharlie » 24 Aug 2022, 22:01

Sorry, I don't understand:

normal normal build: ESP_Easy_mega_20220809_normal_ESP8266_4M1M

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#25 Post by wiredcharlie » 24 Aug 2022, 22:03

Sorry - I have 302 - whatever that is!?

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#26 Post by wiredcharlie » 24 Aug 2022, 22:10

Works with:

ESP_Easy_mega_20220809_normal_ESP8266_4M1M

I didn't notice the "302" when I loaded it. what does it mean?

Thanks

Tony

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

Re: Persistent Variable

#27 Post by TD-er » 24 Aug 2022, 22:46

esp8266/Arduino SDK 3.0.2
Ton is already looking into it, but it seems there is something strange happening with the newest SDK code.

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

Re: Persistent Variable

#28 Post by Ath » 24 Aug 2022, 22:47

I've applied a fix in the PR I opened for this plugin to add the Auto-save option.
The normal 'normal' builds for ESP8266 use the 2.7.4 version of that Core library.
/Ton (PayPal.me)

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: Persistent Variable

#29 Post by wiredcharlie » 27 Aug 2022, 09:59

Thanks for your help.
please don't use delay in rules.
That's an absolute big no-no.
My loop drives a heater. It applies heat for a set period, then waits before checking the effect. The Delay provides a comprehensible way of doing this. I assume the alternative is to break the whole lot up into separate parts triggered by timers - but that would be harder to understand.

What's wrong with Delay? And is there a drop in alternative?

Tony

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

Re: Persistent Variable

#30 Post by Ath » 27 Aug 2022, 10:45

wiredcharlie wrote: 27 Aug 2022, 09:59
please don't use delay in rules.
That's an absolute big no-no.
...
What's wrong with Delay? And is there a drop in alternative?
A delay 'stops the world' in RTOS, the underlying OS of ESPEasy, so disrupts the entire processing of events like 10 per second, 50 per second and once per second, required for a lot of sensors to work as intended. If you use f.e. DS18b20, BME280 or DHT11/12/22 temperature sensors, these heavily rely on the expected timing of those events. If you delay that f.e. by 10 seconds, that would totally confuse/invalidate the measurements.

No drop-in replacement (it would have replaced the delay() function long ago), other than using timers. Proper documentation on what's going on in your code is key, I guess.
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 36 guests