Help with automation condition for unavailable states

Hello, I have an automation that notifies me when household devices like dishwasher/washing machine are done.
The monitoring is done using smart sockets and the automation detects if the power draw drops below a certain value.

However, sometimes the automation triggers at seemingly random times because the state changes from “unavailable” to a value below the threshold. This happens for example if I restart HA or the device has connectivity issues.

I tried to fix this with a condition, but I cannot seem to be able to get the condition right.

This is what I have right now, but the condition always evaluates to false, even if the old state was a number.

alias: Machine done alerts
description: ''
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.washing_machine_power
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 3
  - trigger: numeric_state
    entity_id:
      - sensor.dishwasher_power
    for:
      hours: 0
      minutes: 5
      seconds: 0
    below: 1
conditions:
  - condition: template
    value_template: trigger.from_state.state != "unknown"
actions:
  - action: notify.notify
    metadata: {}
    data:
      message: '{{ trigger.to_state.name }} is done!'
mode: single

Here are some sample trace excerpts:

This one should be ignored

trigger:
  from_state:
    entity_id: sensor.dishwasher_power
    state: unknown
    attributes:
      state_class: measurement
      unit_of_measurement: W
      device_class: power
      friendly_name: Dishwasher Power
  to_state:
    entity_id: sensor.dishwasher_power
    state: '0'
    attributes:
      state_class: measurement
      unit_of_measurement: W
      device_class: power
      friendly_name: Dishwasher Power

This one should not be ignored but is

trigger:
  from_state:
    entity_id: sensor.washing_machine_power_2
    state: '99.1'
    attributes:
      state_class: measurement
      unit_of_measurement: W
      device_class: power
      friendly_name: Washing Machine Power
  to_state:
    entity_id: sensor.washing_machine_power_2
    state: '2'
    attributes:
      state_class: measurement
      unit_of_measurement: W
      device_class: power
      friendly_name: Washing Machine Power

Your value_template is not a template, it should be

value_template: "{{ trigger.from_state.state != "unknown" }}"

The state might be unavailable depending on when it’s rendered so unknown might not work. The function/filter has_value checks for both of these possibilities.

Maybe try something like

value_template: "{{ has_value(trigger.from_state) }}"

(not sure that’ll work) or (and I’m more confident in this)

value_template: "{{ trigger.from_state.state is not in ['unknown', 'unavailable'] }}"

Confirm the previous state value was numeric.

conditions:
  - condition: template
    value_template: "{{ is_number(trigger.from_state.state) }}"

Thank you two! is_number works as expected.
I had no idea the template wasn’t templating all along! :grimacing:

1 Like