Instant pulse sending via MQTT
Moderators: grovkillen, Stuntteam, TD-er
Instant pulse sending via MQTT
Hi,
I'm having a device with a TCRT5000 pulse counter to meassure my kWh & Watt usage.
This is sending pulse count via MQTT to my OpenHAB setup every 5 seconds.
In OpenHAB I've made a rule that does calculate Watt using the time between last pulses and the amount of pulses that have been received (avarage).
This can give me a inaccurate value, since at worst there can be about 9 seconds difference (giving me a 40Watt difference).
Is there a way to send a message to MQTT each time a pulse has been detected (like a switch would)?
I could try add a sendData using a different name on the code to trigger it this way (with a checkbox for example). But I rather not modify the source because it would give me trouble with future updates .
I'm having a device with a TCRT5000 pulse counter to meassure my kWh & Watt usage.
This is sending pulse count via MQTT to my OpenHAB setup every 5 seconds.
In OpenHAB I've made a rule that does calculate Watt using the time between last pulses and the amount of pulses that have been received (avarage).
This can give me a inaccurate value, since at worst there can be about 9 seconds difference (giving me a 40Watt difference).
Is there a way to send a message to MQTT each time a pulse has been detected (like a switch would)?
I could try add a sendData using a different name on the code to trigger it this way (with a checkbox for example). But I rather not modify the source because it would give me trouble with future updates .
Re: Instant pulse sending via MQTT
+1 for this feature!
Re: Instant pulse sending via MQTT
Dec 2015 topic rises from the grave.+1 for this feature!
I believe you can do it with the existing Generic - Pulse counter. The basic idea is to use rules to send the counted value only when it has changed. This calculated value (new total count minus previous total count) is sent via MQTT publish.
Try This:
Setup the Pulse Counter type for Delta, Change. In my example I will call it MeterReader, with default value names (Count, Total, Time). Disable Send to Controller. Set Interval to 1Sec.
Code: Select all
on System#Boot do
let,3,[MeterReader#Total]
endon
on MeterReader#Total do
let,1,[MeterReader#Total]
let,2,[VAR#1]-[VAR#3]
let,3,[VAR#1]
if [VAR#2] > 0
Publish %sysname%/MeterReader/CntVal,[VAR#2] // See note below
endif
endon
I did a quick test with mega-20190630 and it worked correctly for me; Every change to the counter sends the latest count. Data resolution/latency is good (one second updates).
- Thomas
Re: Instant pulse sending via MQTT
Hi Thomas, better to dig up an existing topic then spoiling the forum with double entries
Thanks for your suggestion. I'll surely test it out soon!
I also encountered another problem with pulse counters.
viewtopic.php?f=6&t=6926&p=38441#p38441
Thanks for your suggestion. I'll surely test it out soon!
I also encountered another problem with pulse counters.
viewtopic.php?f=6&t=6926&p=38441#p38441
Re: Instant pulse sending via MQTT
Hi Thomas,
I tried to read and analyse your rule but I'm having a hard time understanding it. I'm not really sure if this is what I need. How to approach this when I only want to send the Total value if it changes (goes up)?
The "Count:" value is not really that useful because my meter sometimes runs so slow (gas) that I would better make use of the Total count.
At the moment espeasy is publishing the total count to a mosquitto broker every 5 seconds but it is spoiling my logs every 5 seconds. At the end of the day (23h59) I'm writing the Total value from Openhab to an influxdb, which gives me the total usage of water on 1 day.
I just tried something simple like this but it doesn't work.
Code: Select all
on H2O#Total do
Publish /sensors/watermeter/%valname%,%value%
endif
endon
Re: Instant pulse sending via MQTT
Then you would use the rules I posted, but change the publish statement to send the Total value. The revised statement would be like this (but change it to match your MQTT):How to approach this when I only want to send the Total value if it changes (goes up)?
Code: Select all
Publish %sysname%/MeterReader/Total,[MeterReader#Total]]
- Thomas
Re: Instant pulse sending via MQTT
Hi Thomas, thanks for your assistance so far. I just think a few things are missing from the rule. The / in front of sensors for example. If I don't add this, the rule is not pusblishing anything to the broker.
Code: Select all
Publish /sensors/watermeter/Total,[VAR#2]
Code: Select all
2019-08-09 09:43:50.926 [vent.ChannelTriggeredEvent] - mqtt:systemBroker:LocalMosquitto:WM triggered 1.00
2019-08-09 09:43:59.926 [vent.ChannelTriggeredEvent] - mqtt:systemBroker:LocalMosquitto:WM triggered 1.00
2019-08-09 10:15:07.922 [vent.ChannelTriggeredEvent] - mqtt:systemBroker:LocalMosquitto:WM triggered 1.00
This is my config of the sensor:
Re: Instant pulse sending via MQTT
Yes, you need to change my Publish example to match your MQTT configuration. For example, the "missing" leading forward slash is needed if your MQTT broker is setup to expect it.I just think a few things are missing from the rule. The / in front of sensors for example. If I don't add this, the rule is not pusblishing anything to the broker.
FWIW, the leading slash is an old school usage, no longer recommended. For an explanation, see the Best Practices section here: https://www.hivemq.com/blog/mqtt-essent ... practices/
The "Publish /sensors/watermeter/Total,[VAR#2]" does not send the Total value, it sends the Count value. That is what I understood the OP wanted.I also only see the value being updated to 1 so I guess the calculation is wrong somewhere?
To send the Pulse Counter's latest Total the statement would be something like this:
Code: Select all
Publish /sensors/watermeter/Total,[watermeter#Total]
- Counter Type: Delta
- Mode Type: Change.
Not sure if it will make a difference, but something to keep in mind if the proposed rules do not work correctly.
BTW, your Pulse Counter Plugin configuration has Decimals set to 2 on the Values. You don't need to publish float values (a fractional counter value will never occur), so just set Decimals to 0 to send integer values instead.
- Thomas
Re: Instant pulse sending via MQTT
Hi Thomas,
This is what I use now but I still think the amount of lines in this rule is too much for what I need?
I don't understand why you need to count [VAR#1]-[VAR#3] and why [VAR#1] needs to be bigger then 0 ? I think the way the rule should work is:
1) store the current value of [watermeter#Total] in a var
2) if the value changes (on watermeter#Total do) and this value is bigger then the previously stored one --> then publish this new value to the controller
== Actually, I don't need to keep track of 2) As I'm sending the Total Count it will always be higher So forget about this
I notice that if I change the "send to controller" value in the Device from 1 to 5 seconds, the entries in the syslog also change from every second to every 5 seconds, so this still has influence.
I'm also using a second pulse counter on the same device and I used the same script as posted above. I think I'm messing up the variables that way because I use the same let 1,2,3
Sorry for sounding noobish, it's like knowing how to speak German but not being able to write it properly
This is what I use now but I still think the amount of lines in this rule is too much for what I need?
Code: Select all
on System#Boot do
let,3,[watermeter#Total]
endon
on watermeter#Total do
let,1,[watermeter#Total]
let,2,[VAR#1]-[VAR#3]
let,3,[VAR#1]
if [VAR#2] > 0
Publish /sensors/watermeter/Total,[watermeter#Total]
endif
endon
1) store the current value of [watermeter#Total] in a var
2) if the value changes (on watermeter#Total do) and this value is bigger then the previously stored one --> then publish this new value to the controller
== Actually, I don't need to keep track of 2) As I'm sending the Total Count it will always be higher So forget about this
I notice that if I change the "send to controller" value in the Device from 1 to 5 seconds, the entries in the syslog also change from every second to every 5 seconds, so this still has influence.
I'm also using a second pulse counter on the same device and I used the same script as posted above. I think I'm messing up the variables that way because I use the same let 1,2,3
Sorry for sounding noobish, it's like knowing how to speak German but not being able to write it properly
Re: Instant pulse sending via MQTT
The result (stored in VAR#2) will be greater than zero when new pulse counts have occurred.I don't understand why you need to count [VAR#1]-[VAR#3]
Because when new counts occur, the VAR#2 value will be 1 or greater. This triggers the publish, otherwise the counter is ignored. This was my suggested workaround to solve the OP's wish: Instant pulse sending via MQTTwhy [VAR#2] needs to be bigger then 0 ?
Feel free to create rules specifically for your project. You are the best person to determine what is needed for your application.I think the way the rule should work is ...
- Thomas
Who is online
Users browsing this forum: Bing [Bot] and 133 guests