Numeric state triggering from "null data"

I have an automation that I want to trigger whenever my hot tub heater turns off. It’s the only element of the hot tub that uses more than 3000W. Everything else uses less than 2000W and the control panel does not allow the heater to be on when all the pumps are running (they add heat to the hot tub anyway). The trigger and condition are:

- id: '1673188189249'
  alias: Write Hot Tub Log
  trigger:
  - platform: numeric_state
    entity_id: sensor.hot_tub_energy_power
    below: '2500'
  condition: []

I thought it was working nicely until I started seeing entries in the log, from the automation triggering, that were not correct. It appeared that the automation was being triggered by something else randomly.
Looking at the history of sensor.hot_tub_energy_power showed just a Zero trace around the times when the automation ran incorrectly. Further, looking at the trace in Grafana showed gaps in the data at those times!
So, clearly the automation is also triggering when, for whatever reason, there is a gap in the data (which is coming every 10s from a Tasmotarized ESP8266 with a PZEM004T sensor through MQTT and the Tasmota Integration), and the data stream suddenly starts up again. I think a dropout of just 1 data point is sufficient to get a trigger!
This must be a common problem with such a numeric_state trigger, so what is the best solution?

Adding a second condition:

{{ ['unavailable','unknown'] not in states('sensor.hot_tub_energy_power') }}

Thanks.
Just tried that in the Template Tool and it said the list was not allowed. Tried with just ‘unavailable’ and that seems to work so will put it in the automation and give it a go and report back in a while.
Edit -
I am assuming I’ve got this right. I’ve put it in as you gave it directly on the condition: line. I saw an example like that in the documentation. Don’t get any errors when I load the automation.yaml.

- id: '1673188189249'
  alias: Write Hot Tub Log
  trigger:
  - platform: numeric_state
    entity_id: sensor.hot_tub_energy_power
    below: '2500'
  condition: '{{ [''unavailable'',''unknown''] not in states(''sensor.hot_tub_energy_power'')  }}'

Will have to wait and see what happens now. The automation should get triggered about once every 2-3 hours.

What do you mean by this:

Do you mean that the value was actually zero or that the value was unavailable/unknown?

If unknown/unavailable a better condition would be:

condition: "{{ trigger.from_state |is_number }}"

No, the automation just triggered but the Trace showed the following error

Error: In 'template' condition: TypeError: 'in <string>' requires string as left operand, not list

Reverting to just ‘unavailable’.

Try my condition.

Tom
I meant that the History graph showed no gaps whereas the Grafana graph did, and at the times I got the incorrect log entries.
Your condition: looks more elegant than Andrew Jones’. I will give it a go. Didn’t know about the trigger.from_state.

Opps. That should be:

condition: "{{ trigger.from_state.state |is_number }}"
1 Like

use Tom;s suggestion, but for reference, this condition should be the other way around:

  condition: >
    {{states('sensor.hot_tub_energy_power') not in ['unavailable','unknown']  }}

And please check your quotes, there are so many double single quotes there…

btw:

{{is_number(trigger.from_state.state)}}

should also work

  1. I’ve used Tom’s suggestion now and the Trace shows it has caught at least one rogue triggering. A genuine one hasn’t come through yet.
  2. Ah yes, that way round makes more sense and should work as well.
  3. I know that you have to switch between ’ and " in these templates but I think it was when I used the GUI Automations screen it replaced some double quotes with two single quotes. That works as well so i gave up fighting it.
  4. Your final suggestion is even better doing away with that Pipe/Filter thing which I don’t really understand too well.

I gave up too but it is really annoying that the Home Assistant GUI violates the guidelines Home Assistant itself defines for properly formatted YAML. And I think the automatically generated quoting style is awful.

It also messes up tidy formatting of the template I use to write the CSV log file. This is the tidy format -

  action:
  - service: notify.hottublog
    data_template:
      message: '
        , {{now().strftime(''%d/%m/%Y'')}}
        , {{now().strftime(''%H:%M:%S'')}}
        , {{states(''sensor.hot_tub_energy_total'')}}
        , {{states(''sensor.hot_tub_energy_total'') | float - states(''input_number.hot_tub_last_energy'') | float}}
        , {{states(''sensor.garagemotion_bme280_temperature'')}}
        , {{states(''sensor.hot_tub_dht11_temperature'')}}
        , {{states(''sensor.hot_tub_temperature_delta'')}}
        , {{states(''sensor.berkeley_castle_grounds_wind_speed'')}}
        , {{states(''sensor.berkeley_castle_grounds_feels_like_temperature'')}}
        , {{states(''sensor.hot_tub_dht11_temperature'') | float - states(''sensor.berkeley_castle_grounds_feels_like_temperature'') | float}}
        , {{(now().timestamp() - states(''input_number.hot_tub_last_time'') | float) / 3600 }}
        , {{(states(''sensor.hot_tub_energy_total'') | float - states(''input_number.hot_tub_last_energy'') | float) / (( now().timestamp() - states(''input_number.hot_tub_last_time'') | float ) / 3600 )}}
        '

If I edit it in the GUI the wrap is replaced in a random fashion!

Because of all that, and the deleting of comments, all of my automations are in Yaml.
So probably not the best person here to advocate Gui editing :wink: