Engine Hours counting when 12v on a particular pin
Moderators: grovkillen, Stuntteam, TD-er
Engine Hours counting when 12v on a particular pin
Hello everyone,
I would like to build an engine hour meter using an esp8266 with an oled display built in. It has to be always on and when it gets +12v on a particular pin to start counting the working hours of an engine. It will always display the hours and then it will report the no. To mqtt (I know that part).
Does anybody has done this before?
Any help would be really appreciated.
I would like to build an engine hour meter using an esp8266 with an oled display built in. It has to be always on and when it gets +12v on a particular pin to start counting the working hours of an engine. It will always display the hours and then it will report the no. To mqtt (I know that part).
Does anybody has done this before?
Any help would be really appreciated.
Re: Engine Hours counting when 12v on a particular pin
The obvious part is you will need a voltage divider to reduce that 12V to (max) 3.3V for an input pin of the ESP. You can then either monitor the GPIO pin, or configure a Switch plugin to pick up the state-change.
Q: How often will the engine turn on and off, like per day or per hour?
To measure the absolute time the engine is on, you'd best enable the NTP option in Tools/Advanced (and configure your timezone and DST settings for accuracy), then measure the differences at the moment the GPIO or switch state changes (using Rules)
You could subtract 1640995200 seconds (January 1st, 2022) from %unixtime% to have a little more manageable numbers, as all you are interested in is the difference from the previous change.
You can use a Generic - Dummy Device for temporary storage (assuming you never loose power on the unit), or to persistently store these values, you can use a Level plugin that can be set using config,task,<taskname>,setlevel,<value>, but be cautious, setting that too often can wear out the flash memory!!! (that's the reason for the question, above
)
To keep the project still a bit interesting/challenging for you, no code examples (yet)
Q: How often will the engine turn on and off, like per day or per hour?
To measure the absolute time the engine is on, you'd best enable the NTP option in Tools/Advanced (and configure your timezone and DST settings for accuracy), then measure the differences at the moment the GPIO or switch state changes (using Rules)
You could subtract 1640995200 seconds (January 1st, 2022) from %unixtime% to have a little more manageable numbers, as all you are interested in is the difference from the previous change.
You can use a Generic - Dummy Device for temporary storage (assuming you never loose power on the unit), or to persistently store these values, you can use a Level plugin that can be set using config,task,<taskname>,setlevel,<value>, but be cautious, setting that too often can wear out the flash memory!!! (that's the reason for the question, above

To keep the project still a bit interesting/challenging for you, no code examples (yet)

/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
If this is a DC motor you plan on monitoring, then please make sure to also add a diode in the "wrong" direction over the 12V.

Example of a diode over a relais coil.
The same should be done for a DC motor as it has a coil inside.
Such a coil may introduce (extremely) high voltage spikes as soon as the current through the coil changes.
Example of a diode over a relais coil.
The same should be done for a DC motor as it has a coil inside.
Such a coil may introduce (extremely) high voltage spikes as soon as the current through the coil changes.
Re: Engine Hours counting when 12v on a particular pin
I’m really thankful for your kind response.
I’m going to measure the engine working hours daily and I have to have a total no. Like 6500h, I’ll have to figure out the rule to add the extra hours every day to the total working hours when the switch turns on and as I can imagine the esp has to always on to keep the existing hours right?
I will start the project and get back with results.
I’m going to measure the engine working hours daily and I have to have a total no. Like 6500h, I’ll have to figure out the rule to add the extra hours every day to the total working hours when the switch turns on and as I can imagine the esp has to always on to keep the existing hours right?
I will start the project and get back with results.
Re: Engine Hours counting when 12v on a particular pin
You could misuse the level plugin to store a value on flash.
This can be stored like this:
config,task,<NameOfTheTask>,setlevel,<ValueToStore>
It can be read like this:
[NameOfTheTask#getValue]
But.... Don't set this value too often as it will save your settings (when checked to save it)
Or you explicitly call "save" from the rules to save the settings.
So when doing this once in 24h will work.
At boot you can restore the value into a variable or dummy task (dummy tasks will keep their value at reboot, as long as the unit remains powered)
This adds a backup feature of the total hours in which you only loose 1 full day at max in your count when the ESP gets power cycled.
As long as you don't decide to save at midnight, but the ESP being powered off at that time every day of course
This can be stored like this:
config,task,<NameOfTheTask>,setlevel,<ValueToStore>
It can be read like this:
[NameOfTheTask#getValue]
But.... Don't set this value too often as it will save your settings (when checked to save it)
Or you explicitly call "save" from the rules to save the settings.
So when doing this once in 24h will work.
At boot you can restore the value into a variable or dummy task (dummy tasks will keep their value at reboot, as long as the unit remains powered)
This adds a backup feature of the total hours in which you only loose 1 full day at max in your count when the ESP gets power cycled.
As long as you don't decide to save at midnight, but the ESP being powered off at that time every day of course

Re: Engine Hours counting when 12v on a particular pin
Thanks again for your help,
I haven't made any progress due to I couldn't figure out how to make the nodemcu 0.96 OLED built-in display to work.
Do you have any suggestion about the device I have to choose? I tried different FW and various devices but couldn't make it work.
Thank you in advance,
Mike
I haven't made any progress due to I couldn't figure out how to make the nodemcu 0.96 OLED built-in display to work.
Do you have any suggestion about the device I have to choose? I tried different FW and various devices but couldn't make it work.
Thank you in advance,
Mike
Re: Engine Hours counting when 12v on a particular pin
That display, assuming it is I2C connected, is supported from the "Display - OLED SSD1306" and "Display - OLED SSD1306/SH1106 Framed" plugins. Both are available in all Normal, Test, Display and Max builds.
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
Nope, Wont work 

Re: Engine Hours counting when 12v on a particular pin
What exact display model do you use? (link to supplier website f.e.)
Is the display I2C connected?
Are the I2C pins configured correctly on the Hardware tab?
Is the display I2C connected?
Are the I2C pins configured correctly on the Hardware tab?
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
That one may need a pin pulled high (or low?) connected to the RST pin of the OLED display. (not the RST of the ESP)
I have seen those before and that's what I remember from them.
Edit:
Just looked at the example code linked on that page:
https://github.com/lspoplove/D-duino/bl ... EDtest.ino
D3 = GPIO-0
D5 = GPIO-14.
Have you used those (absolutely non standard) GPIO settings in ESPEasy?
Can you also perform an I2C scan to see if anything is found?
I have seen those before and that's what I remember from them.
Edit:
Just looked at the example code linked on that page:
https://github.com/lspoplove/D-duino/bl ... EDtest.ino
Code: Select all
// Initialize the OLED display using brzo_i2c
// D3 -> SDA
// D5 -> SCL
D5 = GPIO-14.
Have you used those (absolutely non standard) GPIO settings in ESPEasy?
Can you also perform an I2C scan to see if anything is found?
Re: Engine Hours counting when 12v on a particular pin
I haven’t had the chance to test some things,
I’ll get back with news.
Thank you everyone for your replies
I’ll get back with news.
Thank you everyone for your replies
Re: Engine Hours counting when 12v on a particular pin
So I tried the above settings with no luck, I'm not sure that I am doing the right thing, thought.
Scanning I2C nothing comes up.
![Image]()
Scanning I2C nothing comes up.
- Attachments
-
- Screenshot 2022-06-08 at 21.36.06.png (1013.75 KiB) Viewed 12746 times
Re: Engine Hours counting when 12v on a particular pin
I made a progress,
In Hardware tab, I switched the default I2C Interface pins to:
GPIO ⇄ SDA: D1
GPIO → SCL: D2
Then I scan again for I2C, and it found it.
I created a Device - Display - OLED SSD1306/SH1106 Framed
And it worked!!
I will get back with news.
Thank you everyone for your help.
In Hardware tab, I switched the default I2C Interface pins to:
GPIO ⇄ SDA: D1
GPIO → SCL: D2
Then I scan again for I2C, and it found it.
I created a Device - Display - OLED SSD1306/SH1106 Framed
And it worked!!
I will get back with news.
Thank you everyone for your help.
Re: Engine Hours counting when 12v on a particular pin
Ah good to hear you've got it running now.
Re: Engine Hours counting when 12v on a particular pin
Still stuck in this project.
I am trying to find a rule when I supply 5v to one pin to start counting hours, also to add the already run hours of the system.
Any advice would be really appreciated.
I am trying to find a rule when I supply 5v to one pin to start counting hours, also to add the already run hours of the system.
Any advice would be really appreciated.
Re: Engine Hours counting when 12v on a particular pin
You will have to use a level converter (can be a few resistors to reduce the voltage to 3.3V), as ESP input pins will not handle 5V without damage.
Assumptions:
- A Dummy device is available with name Totals and a value named Runtime
- The input is connected to GPIO 5 (can be changed in the rules)
After adding this code to rules, or changing the GPIO pin, reboot the ESP or run from Tools page this command: event,system#boot
Assumptions:
- A Dummy device is available with name Totals and a value named Runtime
- The input is connected to GPIO 5 (can be changed in the rules)
Code: Select all
On System#Boot Do
Monitor,GPIO,5 // change when using different GPIO pin
Endon
On GPIO#5 Do // change when using different GPIO pin
If %eventvalue1%=1
Let,1,%uptime% // Start of counting, minutes
Else
Let,2,%uptime% // End of counting, minutes
Let,3,([int#2]-[int#1])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Runtime,[Totals#Runtime]+[var#3] // Add run-hours to total runtime and send via controller
Endif
Endon
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
Unfortunately no luck.
When I trigger the GPIO 12 to High (1) and then after some time to Low (0) nothing changes on the Runtime output.
I attach the configuration in case I do something wrong
When I trigger the GPIO 12 to High (1) and then after some time to Low (0) nothing changes on the Runtime output.
I attach the configuration in case I do something wrong
- Attachments
-
- Screenshot 2023-03-05 at 10.53.50.png (522.32 KiB) Viewed 8836 times
-
- Screenshot 2023-03-05 at 10.53.25.png (561.98 KiB) Viewed 8836 times
-
- Screenshot 2023-03-05 at 10.52.44.png (355.16 KiB) Viewed 8836 times
-
- Screenshot 2023-03-05 at 10.28.54.png (507.59 KiB) Viewed 8836 times
Re: Engine Hours counting when 12v on a particular pin
What build version are you running?
It looks a bit old, as we have the build filename located at the lower right corner for quite a while now. (2 years maybe???)
It looks a bit old, as we have the build filename located at the lower right corner for quite a while now. (2 years maybe???)
Re: Engine Hours counting when 12v on a particular pin
Yes, it is the new version, I just didn't include it in the screenshot.
- Attachments
-
- Screenshot 2023-03-05 at 14.49.43.png (909.34 KiB) Viewed 8825 times
Re: Engine Hours counting when 12v on a particular pin
You could use the Switch input plugin event instead of the monitor command, and checking the GPIO:
Remove the entire "On System#Boot Do" event, and change the "On GPIO#12 Do" to this:
Remove the entire "On System#Boot Do" event, and change the "On GPIO#12 Do" to this:
Code: Select all
On EngineSW#onoff Do
If %eventvalue1%=1
Let,1,%uptime% // Start of counting, minutes
Else
Let,2,%uptime% // End of counting, minutes
Let,3,([int#2]-[int#1])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Runtime,[Totals#Runtime]+[var#3] // Add run-hours to total runtime and send via controller
Endif
Endon
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
That worked absolutely perfect!!!
Thank you so much!!! It was something I was trying to do for a year or so.
Now I need to add the already working hours in the rule I guess.

Now I need to add the already working hours in the rule I guess.
- Attachments
-
- IMG_1408.jpg (470.53 KiB) Viewed 8819 times
Re: Engine Hours counting when 12v on a particular pin
Setting the initial value would be as simple as issuing a TaskValueSet,Totals,Runtime,[value] command from the Tools page, but while that would survive a warm boot of the ESP, it won't be preserved on a cold boot/power cycle of the ESP.
That can be solved by using a Regulator - Level Control task, name that: Level
Don't configure a GPIO on that device, and select a non-changing Task/Value pair (you can set the Dummy Device to Dual, and use the second value for that)
Set the initial Runtime to Set level with your value, and the Auto-save interval to a long time, f.e. 720 minutes (12 hours), to reduce the wear of the flash memory. And assuming the ESP will hardly ever loose power or crash. You can set it to a shorter time if it won't keep running for that amount of time.
Then use these adjusted rules:
Changes:
- The Config,task,... command is added
- The On System#Boot Do event is re-introduced
That can be solved by using a Regulator - Level Control task, name that: Level
Don't configure a GPIO on that device, and select a non-changing Task/Value pair (you can set the Dummy Device to Dual, and use the second value for that)
Set the initial Runtime to Set level with your value, and the Auto-save interval to a long time, f.e. 720 minutes (12 hours), to reduce the wear of the flash memory. And assuming the ESP will hardly ever loose power or crash. You can set it to a shorter time if it won't keep running for that amount of time.
Then use these adjusted rules:
Code: Select all
On EngineSW#onoff Do // change when using different GPIO pin
If %eventvalue1%=1
Let,1,%uptime% // Start of counting, minutes
Else
Let,2,%uptime% // End of counting, minutes
Let,3,([int#2]-[int#1])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Runtime,[Totals#Runtime]+[var#3] // Add run-hours to total runtime and send via controller
Config,task,Level,SetLevel,[Totals#Runtime] // Store in Level control, will be auto-saved after the configured interval
Endif
Endon
On System#Boot Do
TaskValueSetAndRun,Totals,Runtime,[Level#GetLevel] // Restore last saved Level value and send via controller (or to display)
Endon
- The Config,task,... command is added
- The On System#Boot Do event is re-introduced
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
OMG I totally lost it now again...
I think I've done exactly what you wrote, with no luck...
Also it wont let me change the Set level value, returns to 1 everytime
I think I've done exactly what you wrote, with no luck...
Also it wont let me change the Set level value, returns to 1 everytime
- Attachments
-
- Screenshot 2023-03-05 at 22.53.39.png (584.81 KiB) Viewed 8801 times
-
- Screenshot 2023-03-05 at 22.53.56.png (857.36 KiB) Viewed 8801 times
-
- Screenshot 2023-03-05 at 22.54.12.png (448.04 KiB) Viewed 8801 times
-
- Screenshot 2023-03-05 at 22.53.23.png (695.42 KiB) Viewed 8801 times
Re: Engine Hours counting when 12v on a particular pin
You should not have changed the Output variable to GetLevel for the Level plugin, that will interfere with the built-in GetLevel special/magic value that will retrieve the current level value. Renaming the Value back to Output should resolve that part.
I think there is no handling of the initial state if the EngineSW is on (1) when the ESP is started, can you check on the Tools/System Variables overview what the current values for Custom Variables %v1%, %v2% and %v3% are? (might be that none is set, but the Custom Variables header will still be shown)
Most likely that will be fixed when the Send Boot state checkbox is enabled.
I think there is no handling of the initial state if the EngineSW is on (1) when the ESP is started, can you check on the Tools/System Variables overview what the current values for Custom Variables %v1%, %v2% and %v3% are? (might be that none is set, but the Custom Variables header will still be shown)
Most likely that will be fixed when the Send Boot state checkbox is enabled.
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
I've set the switch to 1 state before the screenshot.
Although the problem seems to be fixed after changing back, the Output value.
Although the problem seems to be fixed after changing back, the Output value.
- Attachments
-
- Screenshot 2023-03-05 at 23.17.23.png (526.96 KiB) Viewed 8795 times
Re: Engine Hours counting when 12v on a particular pin
Great, we're getting in the right direction
NB:
The Runtime value will only be updated once the Engine is off again, maybe that could/should be improved. I'll think about how to improve that.

NB:
The Runtime value will only be updated once the Engine is off again, maybe that could/should be improved. I'll think about how to improve that.
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
That is not a problem for me, but for someone, it would be great to have a real time status on the display.
Re: Engine Hours counting when 12v on a particular pin
Would it be too much to double the process for the second engine, you think?
- Attachments
-
- IMG_1409.jpg (336.28 KiB) Viewed 8787 times
Re: Engine Hours counting when 12v on a particular pin
Shouldn't be a problem, just duplicate the Devices with different names and GPIO pin, duplicate the EngineSW rule and use different variables in the rules, I'd probably pick 11, 12 and 13 instead of 1, 2 and 3.
The Dummy Device can be set to Triple or Quad, and the TaskValueSetAndRun commands adjusted accordingly ("On System#Boot Do" should only get another TaskValueSetAndRun line added for the extra Level Control you will need to add.)
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
Strangling with the second rule regarding the second engine, I do something wrong.
- Attachments
-
- Screenshot 2023-03-06 at 18.27.56.png (443.9 KiB) Viewed 8731 times
-
- Screenshot 2023-03-06 at 18.27.34.png (472.54 KiB) Viewed 8731 times
-
- Screenshot 2023-03-06 at 18.27.22.png (409.51 KiB) Viewed 8731 times
-
- Screenshot 2023-03-06 at 18.27.10.png (631.61 KiB) Viewed 8731 times
-
- Screenshot 2023-03-06 at 18.26.44.png (585 KiB) Viewed 8731 times
Re: Engine Hours counting when 12v on a particular pin
If next time you could add the script in a Code block in the message (there's a button '</>' in the toolbar above the message edit screen), instead of a screenshot, I could edit it to fix the issues...
There are a few typos in your script:
- The second rule "On System#Boot Do" will never be executed. The line that's in there should be added to the first rule of that name, and the rest of that second rule can be removed
- You calculate var 13 from var 1 and var 2, that should be "Let,13,([int#12]-[int#11])/60"
- The next line, TaskValueSetAndRun, uses [var#3] instead of the desired [var#13]
- The Level Control should both look at the Totals / Value task value, to avoid any unneeded events from the Level Control tasks
Things that didn't need to be changed (but are fine as they are)
- The "onoff" values could both have used that name, as they are in different tasks. It's just that the task names have to be unique, and the value names within a single task too. It is fine as it is now though.
There are a few typos in your script:
- The second rule "On System#Boot Do" will never be executed. The line that's in there should be added to the first rule of that name, and the rest of that second rule can be removed
- You calculate var 13 from var 1 and var 2, that should be "Let,13,([int#12]-[int#11])/60"
- The next line, TaskValueSetAndRun, uses [var#3] instead of the desired [var#13]
- The Level Control should both look at the Totals / Value task value, to avoid any unneeded events from the Level Control tasks
Things that didn't need to be changed (but are fine as they are)
- The "onoff" values could both have used that name, as they are in different tasks. It's just that the task names have to be unique, and the value names within a single task too. It is fine as it is now though.
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
I apologize for that
What if I put the second rule on a Rule set 2?

What if I put the second rule on a Rule set 2?
Code: Select all
On PengineSW#ponoff Do // change when using different GPIO pin
If %eventvalue1%=1
Let,1,%uptime% // Start of counting, minutes
Else
Let,2,%uptime% // End of counting, minutes
Let,3,([int#2]-[int#1])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Pruntime,[Totals#Pruntime]+[var#3] // Add run-hours to total runtime and send via controller
Config,task,Level,SetLevel,[Totals#Pruntime] // Store in Level control, will be auto-saved after the configured interval
Endif
Endon
On System#Boot Do
TaskValueSetAndRun,Totals,Pruntime,[Plevel#GetLevel] // Restore last saved Level value and send via controller (or to display)
Endon
On SengineSW#sonoff Do // change when using different GPIO pin
If %eventvalue1%=1
Let,11,%uptime% // Start of counting, minutes
Else
Let,12,%uptime% // End of counting, minutes
Let,13,([int#12]-[int#11])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Sruntime,[Totals#Sruntime]+[var#13] // Add run-hours to total runtime and send via controller
Config,task,Level,SetLevel,[Totals#Sruntime] // Store in Level control, will be auto-saved after the configured interval
Endif
Endon
On System#Boot Do
TaskValueSetAndRun,Totals,Sruntime,[Slevel#GetLevel] // Restore last saved Level value and send via controller (or to display)
Endon
Last edited by ciscomike on 06 Mar 2023, 19:05, edited 1 time in total.
Re: Engine Hours counting when 12v on a particular pin
Code: Select all
On PengineSW#ponoff Do
If %eventvalue1%=1
Let,1,%uptime% // Start of counting, minutes
Else
Let,2,%uptime% // End of counting, minutes
Let,3,([int#2]-[int#1])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Pruntime,[Totals#Pruntime]+[var#3] // Add run-hours to total runtime and send via controller
Config,task,Level,SetLevel,[Totals#Pruntime] // Store in Level control, will be auto-saved after the configured interval
Endif
Endon
On SengineSW#sonoff Do
If %eventvalue1%=1
Let,11,%uptime% // Start of counting, minutes
Else
Let,12,%uptime% // End of counting, minutes
Let,13,([int#12]-[int#11])/60 // Runtime in hours
TaskValueSetAndRun,Totals,Sruntime,[Totals#Sruntime]+[var#13] // Add run-hours to total runtime and send via controller
Config,task,Level,SetLevel,[Totals#Sruntime] // Store in Level control, will be auto-saved after the configured interval
Endif
Endon
On System#Boot Do
TaskValueSetAndRun,Totals,Pruntime,[Plevel#GetLevel] // Restore last saved Level value and send via controller (or to display)
TaskValueSetAndRun,Totals,Sruntime,[Slevel#GetLevel] // Restore last saved Level value and send via controller (or to display)
Endon
- Combine the "On System#Boot Do" in 1, and move to the end of the file
- Fix the typos in the int/var names
Please test

/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
Both counters seem to work perfect, except that is not keeping the added hours after the power cycle anymore.
Although I have 720 minutes Auto-save interval.
Although I have 720 minutes Auto-save interval.
- Attachments
-
- Screenshot 2023-03-06 at 20.26.36.png (84.81 KiB) Viewed 8685 times
Re: Engine Hours counting when 12v on a particular pin
It will take up to 12 hours (720 minutes) before that setting is saved, you might want to set it to a shorter time, but it should be relatively long, to avoid unwanted wear of the flash memory, eventually causing the ESP to fail, either by save errors or read errors. It will only be saved if the value has changed.
And that checkbox should be unchecked, it will inhibit the time-out value! And do a save immediately after the value is set via the Config command, as it states.
And that checkbox should be unchecked, it will inhibit the time-out value! And do a save immediately after the value is set via the Config command, as it states.
/Ton (PayPal.me)
Re: Engine Hours counting when 12v on a particular pin
Thank you so much.
It is working great!
It is working great!
Re: Engine Hours counting when 12v on a particular pin
Great, thanks for the feedback 

/Ton (PayPal.me)
Who is online
Users browsing this forum: No registered users and 7 guests