Switch and countdown Rules function

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Switch and countdown Rules function

#1 Post by PandCall » 25 Nov 2022, 10:10

Hello,
I need your help, I'm not very good, that's not to be proven anymore, but I'm improving ;)
I'm trying to do something simple but I can't understand how Timer works.
Example
I have a switch
If I learn on it, it activate a lamp (GPIO,13,0) for example and it send a message (MQTT or oled, oledframedcmd,3, "light On")
then I keep GPIO,13,0 in the state during a certain time (30 seconds)
Then at the end of the countdown I switch back the GPIO,13,1 to turn off everything sends a message (MQTT or oled, oledframedcmd,3, "light Off").

I understand that to do this operation, I can not understand how the instructions timerSet, Timer, etc..
Which seems to be the one to use.

With this code

Code: Select all

// Oled Message change on MQTT value (OK)
On Switch_House#State do
  If [Switch_Chaudiere#State]=0
    oledframedcmd,3, "Boiler On
  Else
    oledframedcmd,3, "Boiler Off
  Endif 
Endon 
I can change the message on the Oled, but as soon as I release the switch it goes back to Off, which is normal since I have no notion of time to wait and go to the next instruction.

I know that it must be simple, but my brain blocks on the understanding of the timer to delay a loop.

The mechanism is

Switch = on
Send message and MQTT
Start Countdon
If End Countdown
Switch = Off
Send message and MQTT

It is the Countdown function that is blocking me.

Would you have a simple example to propose to me so that I can understand and apply it and adapt it to my needs?

Regards

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

Re: Switch and countdown Rules function

#2 Post by TD-er » 25 Nov 2022, 10:33

See the "timerset" command: https://espeasy.readthedocs.io/en/lates ... html#timer

Code: Select all

// Oled Message change on MQTT value (OK)
On Switch_House#State do
  If [Switch_Chaudiere#State]=0
    oledframedcmd,3, "Boiler On"
    timerSet,1,10      //Set Timer 1 for the next event in 10 seconds
  Else
    oledframedcmd,3, "Boiler Off"
  Endif 
Endon 

On Rules#Timer=1 do  //When Timer1 expires, do
  // Do whatever is needed here
  ...
endon

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#3 Post by PandCall » 25 Nov 2022, 14:35

Many Thanks for your help

I understood my mistakes,
I have to define in my first function
The number of the timer and its duration.

Then afterwards,
Execute the timer number section with as first variable Rules
Then execute all the instructions that are inside.

I made two big mistakes
I was not executing correctly the
On Rules#Timer=2 do
However, I had tested it, but, there was a second error in my code which did not allow me to understand well.

In my code I made an If ... Else and the Else cancelled the effect of my Timer.
Here is the code that works, I still have to make corrections for the contrast, but the Timer works exactly as I wanted.

Code: Select all

// Oled Message change  on MQTT value (OK)
On Switch_Chaudiere#State do
  If [Switch_Chaudiere#State]=0
    oledframedcmd,3,"Chaudiere On"
      Publish Thermostat/Switch,0
    timerSet,2,10      //Set Timer 2 for the next event in 10 seconds
  Endif 
Endon 

On Rules#Timer=2 do  //When Timer1 expires, do
oledframedcmd,3,"Chaudiere Off"
  Publish Thermostat/Switch,1
Endon
Two things have to go together,
The timerSet,2,10 and the On Rules#Timer=2 do
Otherwise, what we declare is never executed.
Really thanks for these explanations.

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Switch and countdown Rules function

#4 Post by chromo23 » 25 Nov 2022, 16:08

This Post can not be deleted but i assure that it was nonsense :D
Last edited by chromo23 on 25 Nov 2022, 16:13, edited 1 time in total.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#5 Post by PandCall » 25 Nov 2022, 16:10

Finale code withe Local Oled sate and MQTT update and comments

Code: Select all

// Oled Message change  on MQTT value (OK)
On Switch_Chaudiere#State do
  If [Switch_Chaudiere#State]=0				// Check Physical Switch 0 = Pressed
  Publish Thermostat/Switch,1				// MQTT Publish State for NODE-RED Dashboard
    oledframedcmd,3,"Chaudiere On"				// Change Message on local Oled for indicate Switch pressed
    timerSet,2,10				// Set Timer 1 for the next event in 10 seconds
  Endif 
Endon 

On Rules#Timer=2 do				//When Timer2 expires, do
oledframedcmd,3,"Chaudiere Off"				// Change Message on local Oled to indicate Switch state
Publish Thermostat/Switch,0				// MQTT Publish State fot NODE-RED Dashboard
Endon

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#6 Post by PandCall » 25 Nov 2022, 16:17

chromo23,
I don't understand the meaning of your post?

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

Re: Switch and countdown Rules function

#7 Post by TD-er » 25 Nov 2022, 16:17

One small remark...
When having comments in your code, it is best to have the comment matching what you're doing :)

Code: Select all

 timerSet,2,10				// Set Timer 2 for the next event in 10 seconds
Just to help you better understand the rules language;
The first parameter of "timerSet" is the timer ID.
"TimerSet,N,..." matches with the "On Rules#Timer=N do"
With N being a positive integer.
It used to be a limit of 8 timers. Not sure if that limit is still there, but if you see something not working and using this many timers, you may perhaps remember I once mentioned this limit ;)

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

Re: Switch and countdown Rules function

#8 Post by TD-er » 25 Nov 2022, 16:18

PandCall wrote: 25 Nov 2022, 16:17 chromo23,
I don't understand the meaning of your post?
Neither did he ;)

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Switch and countdown Rules function

#9 Post by chromo23 » 25 Nov 2022, 16:22

TD-er wrote: 25 Nov 2022, 16:18Neither did he
:lol:
PandCall wrote: 25 Nov 2022, 16:17 chromo23,
I don't understand the meaning of your post?
I didn´t read your first post thoroughly so i tried to help based on wrong assumptions...and then i deleted the post...

Edit: or at least i tried to.. but once there is a newer post it cannot be deleted anymore, so i changed the text to make that clear.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#10 Post by PandCall » 25 Nov 2022, 16:32

To understand why I was asking this question and why I am doing this.

It's a remote thermostat with the Oled temperature display and a physical switch to force the boiler on for a given time.
All connected to a central developed under node-red.
This allows to know the temperature by individual room and trigger the boiler as needed.
The whole coupled with thermostatic radiator head and controlled remotely.
Thermostat close
Thermostat close
20221125_163408.jpg (604.89 KiB) Viewed 2218 times
Thermostat open
Thermostat open
20221125_163501.jpg (851.11 KiB) Viewed 2218 times

User avatar
chromo23
Normal user
Posts: 821
Joined: 10 Sep 2020, 16:02
Location: germany

Re: Switch and countdown Rules function

#11 Post by chromo23 » 25 Nov 2022, 20:50

Looks interesting....
PandCall wrote: 25 Nov 2022, 16:32 The whole coupled with thermostatic radiator head and controlled remotely.
Can you elaborate this a bit more? I am curious what you did exactly.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#12 Post by PandCall » 25 Nov 2022, 21:00

The project is not completely finished but yes, I will detail the main principles of the project.
Put some pictures of what is in place in prototype mode but which should not change much in its final version.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#13 Post by PandCall » 26 Nov 2022, 18:48

Project
thermostat.png
thermostat.png (375.83 KiB) Viewed 2188 times
Required equipment

1 Raspberry (Pi3, Pi4 + Mosquitto + Node-Red)
1 LCD touchscreen
1 PIR (Infrared motion detector for automatic Screen Off/On)

For MQTT Client
ESP12 and ESP01
DHT22
Switch
Oled SSD1306
Relay Module single
EspEasy

Currently mechanical thermostatic heads, but to change against Zigbee or Z-Wave heads

Node-Red for
Scheduler
Room instructions
Forced operation
hysteresis Etc...
And all the functions to make it work.
And Dashbord for global actions and modification of instructions.

Regards

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

Re: Switch and countdown Rules function

#14 Post by Ath » 26 Nov 2022, 19:34

PandCall wrote: 26 Nov 2022, 18:48 For MQTT Client
ESP12 and ESP01
I'd advise to skip using ESP12/ESP01 with only 1MB Flash, as they are quite hard to upgrade, the ESPEasy builds have grown in functionality, that upgrading these 1MB modules via OTA requires to use a very limited set of plugins.
Easiest is to use Wemos D1 Mini (ESP8266) or Wemos D1 Mini32 (ESP32) clones (these have 4MB of flash and are relatively small).
/Ton (PayPal.me)

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#15 Post by PandCall » 26 Nov 2022, 19:41

Yes I had read that the esp-01 was badly or more supported.
For the relay and the esp-01 I programmed directly via Arduino IDE.
Its function is quite simple
The ESP looks at MQTT, and if the value changes, we activate the relay or not.
So no need for ESPEsy for this part. And an ESP-01 is enough.

Thank you for your warning

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#16 Post by PandCall » 26 Nov 2022, 20:29

Is it possible to interrupt a timer?
I use the switch to start the timer and I test the double click of the same switch to cancel the timer
But it doesn't seem to work during the timer time.
The double click works well, I did tests without Timer
It is not taken into account when a timer is running.
But still, in the Debug console, I can see the double click appearing


2944388: SW: GPIO=13 State=1 Doubleclick=3
2944408: EVENT: Switch_Chaudiere#State=3
2952135: ACT : Publish Thermostat/Switch,1
2952157: ACT : oledframedcmd,3,'Boiler On
2952197: ACT : timerSet,2,00
2952199: TIMER: disable timer

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

Re: Switch and countdown Rules function

#17 Post by Ath » 26 Nov 2022, 20:46

You can set the timer to 0 to disable/cancel that timer, like you seem to do.
/Ton (PayPal.me)

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

Re: Switch and countdown Rules function

#18 Post by Ath » 26 Nov 2022, 20:49

Please share your rules (the logs are informative, but tell nothing about the real rules) if you want advice on that.
/Ton (PayPal.me)

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#19 Post by PandCall » 26 Nov 2022, 20:57

Actions,
I click on the switch
Everything is going well
then I double click to stop the action in progress.
But this has no effect.



Code: Select all

// MQTT Contrast initialisation on Boot (OK)
On System#Boot do
  Publish Thermostat/Switch,0
  timerSet,1,20
endon

// Oled Contrast change  on MQTT value (OK)
On sniff#Oled_switch Do
If [sniff#Oled_switch]=1
oledframedcmd,display,high
Endif
If [sniff#Oled_switch]=0
oledframedcmd,display,low
Endif
If [sniff#Oled_switch]=2
oledframedcmd,display,off
Endif
Endon

// Oled Message change  on MQTT value (OK)
On Switch_Chaudiere#State do
  If [Switch_Chaudiere#State]=0                       // Check Physical Switch 0 = Pressed
  Publish Thermostat/Switch,1                          // MQTT Publish State for NODE-RED Dashboard
    oledframedcmd,3,"Chaudiere On"                // Change Message on local Oled for indicate Switch pressed
    timerSet,2,60                                                // Set Timer 1 for the next event in 10 seconds or more
  Endif 
Endon 

// test Off coundown
on Switch_Chaudiere#State do
 if %eventvalue1%=3
  //double click triggered!
  oledframedcmd,3,"Chaudiere Off"
  Publish Thermostat/Switch,0 
Endon

On Rules#Timer=2 do                                      //When Timer2 expires, do
oledframedcmd,3,"Chaudiere Off"                   // Change Message on local Oled to indicate Switch state
Publish Thermostat/Switch,0                           // MQTT Publish State fot NODE-RED Dashboard
Endon


5021124: DHT : Temperature: 26.50
5021126: DHT : Humidity: 51.50
5021149: EVENT: DHT22#Temperature=25.24
5021158: EVENT: DHT22#Humidity=51.50
5023069: WD : Uptime 84 ConnectFailures 0 FreeMem 13752 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
5031124: DHT : Temperature: 26.50
5031126: DHT : Humidity: 51.50
5031148: EVENT: DHT22#Temperature=25.24
5031158: EVENT: DHT22#Humidity=51.50
5038666: SW : GPIO=13 State=0 Output value=0
5038702: EVENT: Switch_Chaudiere#State=0
5038721: ACT : Publish Thermostat/Switch,1
5038743: ACT : oledframedcmd,3,'Chaudiere On'
5038782: ACT : timerSet,2,60
5038816: IMPT : [sniff#Oled_switch] : 1.00
5038818: EVENT: sniff#Thermostat/Switch=1
5038823: EVENT: sniff#Oled_switch=1.00
5038841: ACT : oledframedcmd,display,high
5038867: EVENT: Oled#contrast=2
5038875: SW : GPIO=13 State=1 Output value=1
5038898: EVENT: Switch_Chaudiere#State=1
5041124: DHT : Temperature: 26.50
5041126: DHT : Humidity: 51.50
5041151: EVENT: DHT22#Temperature=25.24
5041160: EVENT: DHT22#Humidity=51.50
5051124: DHT : Temperature: 26.50
5051126: DHT : Humidity: 52.20
5051148: EVENT: DHT22#Temperature=25.24
5051158: EVENT: DHT22#Humidity=52.20
5052165: SW : GPIO=13 State=0 Output value=0
5052185: EVENT: Switch_Chaudiere#State=0
5052199: ACT : Publish Thermostat/Switch,1
5052227: ACT : oledframedcmd,3,'Chaudiere On'
5052264: ACT : timerSet,2,60
5052316: SW : GPIO=13 State=1 Output value=1
5052337: EVENT: Switch_Chaudiere#State=1
5052366: SW : GPIO=13 State=0 Output value=0
5052385: EVENT: Switch_Chaudiere#State=0
5052399: ACT : Publish Thermostat/Switch,1
5052422: ACT : oledframedcmd,3,'Chaudiere On'
5052458: ACT : timerSet,2,60
5052486: IMPT : [sniff#Oled_switch] : 1.00
5052487: EVENT: sniff#Thermostat/Switch=1
5052495: EVENT: sniff#Oled_switch=1.00
5052512: ACT : oledframedcmd,display,high
5052539: EVENT: Oled#contrast=2
5052614: SW : GPIO=13 State=1 Doubleclick=3
5052635: EVENT: Switch_Chaudiere#State=3
5052874: IMPT : [sniff#Oled_switch] : 1.00
5052875: EVENT: sniff#Thermostat/Switch=1
5052881: EVENT: sniff#Oled_switch=1.00
5052892: ACT : oledframedcmd,display,high
5052923: EVENT: Oled#contrast=2
5053070: WD : Uptime 84 ConnectFailures 0 FreeMem 13424 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
5061124: DHT : Temperature: 26.50
5061126: DHT : Humidity: 52.40
5061149: EVENT: DHT22#Temperature=25.24
5061158: EVENT: DHT22#Humidity=52.40
5063680: EVENT: Clock#Time=Sat,20:54
5071124: DHT : Temperature: 26.60
5071126: DHT : Humidity: 52.30
5071150: EVENT: DHT22#Temperature=25.33
5071159: EVENT: DHT22#Humidity=52.30

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

Re: Switch and countdown Rules function

#20 Post by Ath » 26 Nov 2022, 21:25

Code: Select all

// MQTT Contrast initialisation on Boot (OK)
On System#Boot do
  Publish Thermostat/Switch,0
  timerSet,1,20
endon

// Oled Contrast change  on MQTT value (OK)
On sniff#Oled_switch Do
  If %eventvalue1%=1
    oledframedcmd,display,high
  Endif
  If %eventvalue1%=0
    oledframedcmd,display,low
  Endif
  If %eventvalue1%=2
    oledframedcmd,display,off
  Endif
Endon

// Oled Message change  on MQTT value (OK)
On Switch_Chaudiere#State do
  If %eventvalue1%=0                       // Check Physical Switch 0 = Pressed
    Publish Thermostat/Switch,1                          // MQTT Publish State for NODE-RED Dashboard
    oledframedcmd,3,"Chaudiere On"                // Change Message on local Oled for indicate Switch pressed
    timerSet,2,60                                                // Set Timer 1 for the next event in 10 seconds or more
  Endif 
  if %eventvalue1%=3
    //double click triggered!
    oledframedcmd,3,"Chaudiere Off"
    Publish Thermostat/Switch,0 
  endif
Endon 

On Rules#Timer=2 do                                      //When Timer2 expires, do
  oledframedcmd,3,"Chaudiere Off"                   // Change Message on local Oled to indicate Switch state
  Publish Thermostat/Switch,0                           // MQTT Publish State fot NODE-RED Dashboard
Endon
There were a few issues in your code:
- Duplicate events: Because of caching this won't be handled as it might look, have to avoid that
- An if without an endif, unlike C, there's no single-statement executed as there is no begin/end execution block, so the endif is required
/Ton (PayPal.me)

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Switch and countdown Rules function

#21 Post by PandCall » 26 Nov 2022, 22:09

Oops, yeah I didn't see that.
I check my conditions

Post Reply

Who is online

Users browsing this forum: Jieffe and 31 guests