I am trying to use a sensor that collects total rainfall using MQTT and publish an MQTT message with its value every so I can compute current daily rainfall = total - last night. To test, (I think ) I created a trigger automation that fires every minute and publishes the current total.
It never fires…
Here is my code…
- id: "cumrain99"
alias: record cumulative rain every midnight
description: Roll over yesterdays cumulative rain as day-1
trigger:
- platform: time_pattern
minutes: "/1"
condition: []
action:
- service: mqtt.publish
data:
payload: '{"day_1":"{{states(sensor.rainfall_total_nj)}}"}'
topic: rtl_433/prior_days_rain
mode: parallel
I expected to see an MQTT message on topic rtl_433/prior_days_rain
with a payload of
day_1:xx.x
Where xx.x is the current rainfall total.
When I listen for any messages in mqtt (topic: # ) it never appears, and the template sensor I use to compute the current rainfall is always unknown
Many thanks, I am sure it will work now. I seem to now be gettting the current rain values in MQTT. Next to make sure the daily value is calculated correctly. I should have known I had to escape the double quotes. Next time.
Here’s another way to publish the data in JSON format without concerning oneself with what does/doesn’t need to be escaped.
In the following example, the variable rainfall is defined as a YAML dictionary containing a single key-value pair where day_1 is the key and the state value of sensor.rainfall_total_nj is the key’s value.