Automation trigger not firing and sending MQTT message

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

Any hints?

Yup your issue is here:

        payload: '{"day_1":"{{states(sensor.rainfall_total_nj)}}"}'

It needs to be:

        payload: "{\"day_1\":\"{{ states('sensor.rainfall_total_nj') }}\"}"

As you can see the important part you were missing is that the states function needs the sensor to be in quotes.

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.

You don’t if the outer quotes are different from the inner quotes.

You do if the outer and inner quotes are the same.

Yes, I knew that, but missed that the sensor value had to be in single quotes. Still learning.

You knew you did not have to escape the double-quotes yet said you had to escape them? :thinking:

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.

    - variables:
        rainfall:
          day_1: "{{ states('sensor.rainfall_total_nj') }}"

All that’s needed to ensure the variable rainfall is published as a JSON dictionary is to use the to_json filter.

        payload: "{{ rainfall | to_json }}"

EXAMPLE

- 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:
    - variables:
        rainfall:
          day_1: "{{ states('sensor.rainfall_total_nj') }}"
    - service: mqtt.publish
      data:
        payload: "{{ rainfall | to_json }}"
        topic: rtl_433/prior_days_rain
  mode: parallel

Screenshot from MQTT Explorer

Shows the payload was received in JSON format as {"day_1":7}

1 Like