Instant pulse sending via MQTT

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Kennie
New user
Posts: 1
Joined: 03 Dec 2015, 15:41

Instant pulse sending via MQTT

#1 Post by Kennie » 03 Dec 2015, 15:50

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 :).

bulthaup
Normal user
Posts: 49
Joined: 29 Jul 2019, 15:10

Re: Instant pulse sending via MQTT

#2 Post by bulthaup » 01 Aug 2019, 14:03

+1 for this feature!

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Instant pulse sending via MQTT

#3 Post by ThomasB » 01 Aug 2019, 20:27

+1 for this feature!
Dec 2015 topic rises from the grave.

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
Note: The variable VAR#2 holds the latest count and it is sent via MQTT publish. The Publish statement will need to be edited to suit your MQTT configuration.

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

bulthaup
Normal user
Posts: 49
Joined: 29 Jul 2019, 15:10

Re: Instant pulse sending via MQTT

#4 Post by bulthaup » 08 Aug 2019, 20:46

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

bulthaup
Normal user
Posts: 49
Joined: 29 Jul 2019, 15:10

Re: Instant pulse sending via MQTT

#5 Post by bulthaup » 08 Aug 2019, 22:51

ThomasB wrote: 01 Aug 2019, 20:27 The basic idea is to use rules to send the counted value only when it has changed.
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

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Instant pulse sending via MQTT

#6 Post by ThomasB » 08 Aug 2019, 23:03

How to approach this when I only want to send the Total value if it changes (goes up)?
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):

Code: Select all

Publish %sysname%/MeterReader/Total,[MeterReader#Total]]
The Total value will only be sent when the counter's values change.


- Thomas

bulthaup
Normal user
Posts: 49
Joined: 29 Jul 2019, 15:10

Re: Instant pulse sending via MQTT

#7 Post by bulthaup » 09 Aug 2019, 10:20

ThomasB wrote: 08 Aug 2019, 23:03 The Total value will only be sent when the counter's values change.
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]
I also only see the value being updated to 1 so I guess the calculation is wrong somewhere? These are logs from Openhab, with every update the value just gets overwritten by 1 again. Though the current Total count is 7.
Capture12.PNG
Capture12.PNG (2.12 KiB) Viewed 4807 times

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:
Capture.PNG
Capture.PNG (36.74 KiB) Viewed 4807 times

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Instant pulse sending via MQTT

#8 Post by ThomasB » 09 Aug 2019, 18:15

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.
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.

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/
I also only see the value being updated to 1 so I guess the calculation is wrong somewhere?
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.

To send the Pulse Counter's latest Total the statement would be something like this:

Code: Select all

Publish /sensors/watermeter/Total,[watermeter#Total]
When I tested my example rules I used different Pulse Counter configuration than yours. I set:
- 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

bulthaup
Normal user
Posts: 49
Joined: 29 Jul 2019, 15:10

Re: Instant pulse sending via MQTT

#9 Post by bulthaup » 16 Aug 2019, 21:23

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?

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
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 :)

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Instant pulse sending via MQTT

#10 Post by ThomasB » 16 Aug 2019, 23:35

I don't understand why you need to count [VAR#1]-[VAR#3]
The result (stored in VAR#2) will be greater than zero when new pulse counts have occurred.
why [VAR#2] needs to be bigger then 0 ?
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 MQTT
I think the way the rule should work is ...
Feel free to create rules specifically for your project. You are the best person to determine what is needed for your application.

- Thomas

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests