My automation does not trigger on value change of my template sensor

I am still inexperienced with writing automations and require help:

I have a dishwasher that is connect to a smart plug with power-meter and I want to turn it off automatically, because it uses up to 10W in standby. (Which is insane!)

I have defined this template sensor to define a state sensor with status standby in configuration.yaml:

template:
  - sensor:
      - name: "kueche__spuelmaschine_status"
        unique_id: "kueche_spuelmaschine_status_template"
        state: >
          {% if states('sensor.kueche_steckdose_spuelmaschine_power') | float(0) < 1 %}
            off
          {% elif states('sensor.kueche_steckdose_spuelmaschine_power') | float(0) > 11 %}
            running
          {% else %}
            standby
          {% endif %}

I have confirmed that the sensor-status is correctly reported in the HA GUI depending on the dishwasher power-usage.

I defined the following automation for turning the dishwasher off, if it is in status standby for 30 minutes.
But it is not triggering for some reason and I do not understand why:

alias: automation__kueche__spuelmaschine_ausschalten
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.kueche_spuelmaschine_status
    from: null
    to: standby
    for:
      hours: 30
      minutes: 0
      seconds: 0
conditions: []
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.kueche_steckdose_spuelmaschine
mode: single

Any help is greatly appreciated!

The entity id in your sensor and automation probably does not match. While I have no idea what the actual entity id for your sensor is (as it can be changed from the UI), the one that would be autogenerated from this name has two underscores after “kueche” while the entity id specified in the automation has one.

Thanks for suggesting this. I checked in the HA GUI and the entity id is reported as

sensor.kueche_spuelmaschine_status

So that should not be the issue.

I usually use the double underscores in the names for clear separation, which HA converts to single underscore for entity ids. (I believe because double underscore is not valid for variable names yaml(?))

First what does the trace say…second your automation says 30 HOURS.

3 Likes

In addition to NathanCu’s comments, I’d also verify this is what you want for the trigger. Does your sensor actually have a null state?

The From is optional. Null ignores attribute changes so I avoid it.

3 Likes

This was part of the solution, so I accept it as solution. Thank you!

I messed this up: It was supposed to be 30 minutes.

Additionally, I also found that the smart plug was not reporting the change in power-consumption reliably. This was because the reporting-settings shown Zigbee2MQTT were to coarse, so that it would not report a change with a Delta >5 Watts.

This worked, when the dish-washer was in actually running and then went to standby (after finishing the cleaning) - this reported a status change for the template sensor. But it the smart-plug did not report a change in power consumption (and hence did not trigger the template sensor and automation) while testing, because there I just turned on the smart-plug (i.e. going from off to standby). In that case the Delta is below 5 Watts and hence the smart-plug did not update the power-consumption to a value >0W and hence the power-status of the template-sensor was not updated. (I hope this explanation is somewhat comprehensible.)

I now changed this and it now works as expected. (Testing is ongoing.)

1 Like

Thanks for the reply. Yes, the null had also caught my attention. I created the automation in GUI, where the source-state is set to ANY. This corresponds to the null value in YAML.

I chose ANY for source-state, because I want the automation to trigger, whenever the machine status goes to standby and the template-sensor has two potential source-states for that (off and on). (The trigger does not have null state.)

1 Like