Timer: convert seconds in minutes

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Timer: convert seconds in minutes

#1 Post by fredfit » 31 Mar 2021, 15:19

Hi all, thank you for welcoming me on this board !

I have a little project based on an ESP8266 and an OLED display. They are both integrated in the same PCB:
https://www.banggood.com/fr/Nodemcu-Wif ... 54759.html

I'm learning how to use ESPeasy and have been looking into the "Rules" possibilities.
I have a simple request for which I need your help:
I declared 2 devices, one switch and one OLED SSD1306. I'm now trying to display a countdown of 1 hour.
I put the following rule which works well (I know, it's very basic but I'm learning!):

Code: Select all

On Pushbutton#Statepush do
  If [Pushbutton#Statepush]=0
    TimerSet,1,1
    Let,1,3600
  Else
    TimerSet,1,0
  Endif
Endon 
  
On Rules#Timer=1 Do
  Let,1,[VAR#1]-1
  If [VAR#1]=0 //coundown stops at 0
    TimerSet,1,0
  Else
    TimerSet,1,1
  Endif
EndOn
I can trigger my countdown by pushing on the pushbutton and get the countdown running on the display (with "[VAR#1] sec" in one line of the OLED settings).
But I now would like that it's displayed in minutes, not seconds. So I'm seeking how to convert seconds in minutes.
How can I do that ?

Many thanks in advance!

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

Re: Timer: convert seconds in minutes

#2 Post by TD-er » 31 Mar 2021, 16:18

I did edit your post to make the posted code a bit better readable as the indentation made it hard to read.

First one tip to prevent false pulse detections in the future.

Code: Select all

On Pushbutton#Statepush do
  If [Pushbutton#Statepush]=0
Please keep in mind that the events to be processed in rules are placed in a queue.
So there is some delay between the event being placed in the queue and actually being processed.
So the actual state of [Pushbutton#Statepush] may already have been changed.
Every event does get some (upto 4) event values along with it.
These can be used in your rules via %eventvalue1% ... %eventvalue4%

Code: Select all

On Pushbutton#Statepush do
  If %eventvalue1%=0

I assume you're using the Framed OLED plugin.
Please have a look at its commands: https://espeasy.readthedocs.io/en/lates ... -available

On builds like "custom" or "normal" there is a system variables page, showing you the actual state of all variables and also some standard conversions.

Code: Select all

Mins to days: %c_m2day%(1900)
Mins to days: 1.32	Mins to days: 1.32
Mins to dh:   %c_m2dh%(1900)
Mins to dh: 1d07h	Mins to dh: 1d07h
Mins to dhm:  %c_m2dhm%(1900)
Mins to dhm: 1d07h40m	Mins to dhm: 1d07h40m
Secs to dhms: %c_s2dhms%(100000)
Secs to dhms: 1d03:46:40	Secs to dhms: 1d03%3a46%3a40
So you could use %c_s2dhms%([var#1]) in the command to display text on your OLED.
You can do it every second, when you decrease the variable.
Or add an extra check like this:

Code: Select all

if [var#1]%60=0
  oledframedcmd,1,[var#1]/60
endif
Not sure if the calculation will be done correctly on that line, otherwise you can store it in a new variable using let.

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#3 Post by fredfit » 31 Mar 2021, 16:26

Thank you very much TD-er for this very detailed and constructive reply!
I will carefully study all this ... after having taken an aspirin ;)
Good to see that the support is so efficient on this board!

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#4 Post by fredfit » 31 Mar 2021, 17:00

TD-er, I did some tests, it's nearly what I was expecting ... but nearly only:
Yes, I'm using Framed OLED plugin.
By putting

Code: Select all

%c_s2dhms%([var#1])
in the command to display text on the OLED,I get the following kind of display:

0d00:56:52 for example
that I translate by 0 day, 00 hour, min, sec.

Considering that my counter will never exceed one hour, I would like it to only display minutes and seconds like this: 56:52
Any possibility to do that?

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

Re: Timer: convert seconds in minutes

#5 Post by Micha_he » 31 Mar 2021, 20:46

Maybe with SubString in a Rule!? Look here: https://espeasy.readthedocs.io/en/lates ... Substring#

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

Re: Timer: convert seconds in minutes

#6 Post by Ath » 31 Mar 2021, 20:50

This works for me:

Code: Select all

{substring:5:10:'%c_s2dhms%([Var#1])'}
NB: It needs the time string quoted as substring gets confused by the extra colons that are in there.
/Ton

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

Re: Timer: convert seconds in minutes

#7 Post by TD-er » 31 Mar 2021, 20:56

Like I showed, you can also store the div and mod in different variables and format those to a string to be displayed on the OLED.

Code: Select all

let,2,[var#1]%60  // seconds
let,3,[var#1]/60  // minutes
oledframedcmd,1,"[var#3]:[var#2#D1]"
See: https://espeasy.readthedocs.io/en/lates ... red-values

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

Re: Timer: convert seconds in minutes

#8 Post by Ath » 31 Mar 2021, 21:05

TD-er wrote: 31 Mar 2021, 20:56

Code: Select all

let,2,[var#1]%60  // seconds
let,3,[var#1]/60  // minutes
oledframedcmd,1,"[var#3]:[var#2#D1]"
That does seem to cause rounding issues, when testing with 3599 seconds, this is the result: 59.9833333333334:59
Last edited by Ath on 31 Mar 2021, 21:07, edited 1 time in total.
/Ton

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

Re: Timer: convert seconds in minutes

#9 Post by TD-er » 31 Mar 2021, 21:07

Also when using [int#...] instead of [var#..] ?

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

Re: Timer: convert seconds in minutes

#10 Post by Ath » 31 Mar 2021, 21:11

This works as intended:

Code: Select all

  let,1,3599%60
  let,2,3599/60
  logentry,"[var#2#F]:[var#1#D2]"
Edit: [Int#2] still does standard rounding, that's why the #F (floor) transformation is used
/Ton

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#11 Post by fredfit » 01 Apr 2021, 09:34

Thank you all for your help!
The substring method given by Ath does the job perfectly ;)
I will also consider what TD-er suggested, that would improve my learning on ESPeasy

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#12 Post by fredfit » 01 Apr 2021, 10:53

I now have to think of implementing the following scenario:
- display normally off
- display goes on and countdown starts on pressing the pushbutton
- display goes off at the end of the countdown
- optional: countdown stops prematurely and display goes off by long press on the pushbutton

I will use oledframedcmd,display,<value> for the on/off and clear commands
I know how to use these commands with a http request but how do you use it in the rules ?
Any suggestion welcome!

For long press on the pushbutton, it's described in the rules documentation, I might be able to manage it by myself:

Code: Select all

on Button#State=1 do
 timerSet,1,1
endon

on rules#timer=1 do
 if [Button#State]=0
  //Action if button is short pressed
 else
  //Action if button is still pressed
 endif
endon

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

Re: Timer: convert seconds in minutes

#13 Post by TD-er » 01 Apr 2021, 13:56

Longpress does give another event value.
So I strongly advice you to test with

Code: Select all

logentry,"Bladiebla eventvalue1: %eventvalue1%"
This will help you a lot in debugging how values are sent along with events.

The Framed OLED plugin also supports handling a button set in the plugin, which can be used in the plugin itself to set a timer and turn the display off after some time.

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

Re: Timer: convert seconds in minutes

#14 Post by Ath » 01 Apr 2021, 14:29

My take on a similar subject: Project: PV Monitor

There are also examples of rules using oledframed commands. The PR's (pull requests) that are mentioned there are already included in current ESPEasy releases. I haven't updated that unit in quite some time (ESPEasy can be quite low maintenance :D) so I'm not sure if the plugin combo OLedFramed and APDS-9660 is still in1 build, I'll have to check that out.
/Ton

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#15 Post by fredfit » 03 Apr 2021, 16:21

Hi,
I'm almost there with this code:

Code: Select all

On Pushbutton#Statepush do
If %eventvalue1%=0
TimerSet,1,1
Let,1,3600
oledframedcmd,display,on
oledframedcmd,display,med
Else
TimerSet,1,0
oledframedcmd,display,off
Endif
Endon 
  
On Rules#Timer=1 Do
Let,1,[VAR#1]-1
If [VAR#1]=0 //coundown stops at 0
TimerSet,1,0
oledframedcmd,display,off
Else
TimerSet,1,1
Endif
EndOn
But at the end of the countdown, the display goes off for a second and on again. I cannot figure out why. Please help!
I know that my code is probably not at its best. I tried to improve it with your advices, but that's all I ended up to.

By the way, as stated in this tutorial https://projetsdiy.fr/espeasy-ssd1306-d ... 0-esp8266/ (sorry, in French), there are some http requests to act on the display, especially one to clear it:

Code: Select all

http://IP address>/control?cmd=oledcmd,clear
I cannot find in the Rules documentation the equivalent to be used in a rule.
oledframedcmd,display,clear does nothing.
Any clue on this ?

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

Re: Timer: convert seconds in minutes

#16 Post by Ath » 03 Apr 2021, 17:01

fredfit wrote: 03 Apr 2021, 16:21 But at the end of the countdown, the display goes off for a second and on again. I cannot figure out why. Please help!
...
I cannot find in the Rules documentation the equivalent to be used in a rule.
Is the data on your OLed set from an external source? In that case you should uncheck the checkbox 'Wake display on receiving text:'
And I think that you should not enable the 'Display button' and 'Display Timeout' settings.

For OLedFramed there is no command to clear the display. Usually it is configured to display values using the Line 1 through Line 12 template, clearing the display and having it filled again in a short while would be a bit weird, I guess.
It does seam reasonable to add that as a command, I've worked on that plugin before, so I'll see what I can do.
/Ton

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#17 Post by fredfit » 03 Apr 2021, 18:31

Thanks Ath!
The display does not receive any text or whatever from an external source. It only displays the counter set in the rule and a date/date as alternate header. I tried to disable it, same result.
I just display on LIne 2 "{substring:5:10:'%c_s2dhms%([Var#1])'}"
"Wake display on receiving text" is already unchecked, as well as "Step through frames with Display button" and "Display Timeout" is set to 0.
The ESP8266 is running "ESP_Easy_mega_20210223_normal_ESP8266_4M1M" firmware version.

Thank you if you can add a clear command, but in my case, it doesn't seems to be needed to run a fresh display at coundown start.

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#18 Post by fredfit » 07 Apr 2021, 16:52

I'm still struggling with the oledframedcmd,display,off command.
I did a very simple test by just putting this rule in place:

Code: Select all

on System#Boot do
oledframedcmd,display,off
endon
I was hoping that my board would boot with a blank screen ... but no!
Even that simple rule doesn't work, I'm desperate :shock:
What am I missing with this command ?

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

Re: Timer: convert seconds in minutes

#19 Post by Ath » 07 Apr 2021, 16:57

Well, the command most likely works, but the display probably isn't initialized at that moment, so after that event the 'normal' initialization, including the Logo display, is done.
I think the earliest event you can do something with the display is the <task>#Init event:

Code: Select all

on OLed#init do
  OLedFramedCMD,display,off
endon
This assumes your display task is named 'OLed'.
I have not tested this solution, though. :)

Maybe this plugin needs a setting to not display anything at startup.
/Ton

fredfit
Normal user
Posts: 11
Joined: 31 Mar 2021, 14:59

Re: Timer: convert seconds in minutes

#20 Post by fredfit » 08 Apr 2021, 16:19

Thank you Ath.
Unfortunately, the code you have given doesn't improve the behaviour.

But I finally had some doubts about my board (integrated ESP8266 + OLED). So I tried the exact same configuration and rule with a Wemos D1 mini and an external OLED Display (SSH1106 instead, but this model is supported by ESPeasy) and some quick wiring between them:
I don't face any issue with the oledframedcmd,display,off command anymore! :D :D :D
I guess that something in the integrated board's hardware forces the display to always be on (a pullup resistor ?). That's a pity, I will try to find what and remove it.

Anyway, here I am with what I was desperatly tring to do! Thanks again very much to those who helped me.

My final code is:

Code: Select all

On System#Boot do
  TaskValueSet,4,1,1 //initialize dummy value
  oledframedcmd,display,off
Endon

On Switch#State do
 If %eventvalue%=0
    oledframedcmd,display,on
    oledframedcmd,display,med
  TaskValueSet,4,1,%eventvalue%
   If [Dummy#Statedummy]=0
    TimerSet,1,1
    Let,1,3600
   Else
    TimerSet,1,0
   Endif
 Else
  TaskValueSet,4,1,1
 Endif
Endon 

  
On Rules#Timer=1 Do
 Let,1,[VAR#1]-1
 If [VAR#1]=0 //coundown stops at 0
  oledframedcmd,display,off
  TimerSet,1,0
 Else
  TimerSet,1,1
 Endif
EndOn

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

Re: Timer: convert seconds in minutes

#21 Post by TD-er » 08 Apr 2021, 16:42

I've seen that not all OLED boards are the same.
For example some don't support to set the brightness and some don't support all brightness levels.
On some you may even hear some typical coil whine when using a particular brighness value.
Maybe something similar is different on the board you're using, or maybe the used chip is slightly different (while still being sold as being the 1306)

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests