Trigger templates

Hello and a nice weekend to everyone reading this! Following Issue: I have a Tasmota Smart Plug to sensor the power draw of my washing mashine and yesterday I thought about using that sensor (“sensor.tasmota_waschmaschine_energy_power”) to measure if its currently running or not. The idea was to turn the Smart plug off at 8pm if its not doing its thing right now. Now the Problem: the washing mashine doesnt draw constantly high amounts of power during its washing phase. In fact, sometimes it does absolutely nothing in terms of power consumption, even though it has got an entire hour of work left.
To take care of that, I tried using a template trigger with a 10min “for” time value, to monitor if “sensor.tasmota_waschmaschine_energy_power” is really at idle. But that doesn´t work, because the template trigger needs a change in state to be triggered and if it was not coincidentally running right before 8pm the automation would not be triggered.
Here is my automation and thanks in advance:

alias: Waschmaschine_Zeitschaltung
description: “”
trigger:

  • platform: template
    value_template: “{{states("sensor.tasmota_waschmaschine_energy_power")|int < 10}}”
    for:
    hours: 0
    minutes: 10
    seconds: 0
    enabled: true
    condition:
  • condition: time
    after: “20:00:00”
    before: “07:00:00”
    action:
  • type: turn_off
    device_id: c3fa52a362f4a3a280bc5a373cd95d91
    entity_id: switch.tasmota_waschmaschine
    domain: switch
  • wait_for_trigger:
    • platform: time
      at: “06:55:00”
  • type: turn_on
    device_id: c3fa52a362f4a3a280bc5a373cd95d91
    entity_id: switch.tasmota_waschmaschine
    domain: switch
    mode: single
  1. Use the state trigger instead of the template trigger: The template trigger can be inefficient because it needs to evaluate the template every time the state of any entity changes in Home Assistant. Using the state trigger with a filter on the washing machine’s power consumption would be more efficient.
  2. Use a time trigger instead of wait_for_trigger: The wait_for_trigger action is useful in some cases, but in this automation, it would be simpler to use a time trigger to turn on the washing machine at a specific time.
  3. Use service instead of type for actions: The type action has been deprecated in Home Assistant and should be replaced with the `service’ action
1 Like

alias: Waschmaschine Zeitschaltung
description: ""
trigger:
  - platform: state
    entity_id: sensor.tasmota_waschmaschine_energy_power
    below: 10
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition:
  - condition: time
    after: "20:00:00"
    before: "07:00:00"
action:
  - service: switch.turn_off
    target:
      entity_id: switch.tasmota_waschmaschine
  - delay:
      hours: 10
      minutes: 55
  - service: switch.turn_on
    target:
      entity_id: switch.tasmota_waschmaschine
mode: single

1 Like

That is a good point. If there is a chance that Home Assistant might restart during the waiting period, it is better to consider an alternative approach. One option could be to use a time-based trigger instead of the state trigger.


alias: Waschmaschine Zeitschaltung
description: ""
trigger:
  - platform: time
    at: "06:55:00"
condition:
  - condition: state
    entity_id: sensor.tasmota_waschmaschine_energy_power
    below: 10
  - condition: time
    after: "20:00:00"
    before: "07:00:00"
action:
  - service: switch.turn_off
    target:
      entity_id: switch.tasmota_waschmaschine
  - delay:
      hours: 10
      minutes: 55
  - service: switch.turn_on
    target:
      entity_id: switch.tasmota_waschmaschine
mode: single

This automation triggers every day at 6:55 AM, which eliminates the need for a long wait period. The automation then checks if the washing machine power is below 10 W and if the current time is between 8 PM and 7 AM. If both conditions are met, the automation turns off the washing machine and waits for 10 hours and 55 minutes before turning it back on.

1 Like

Thanks for helping me out. I cant use (or at least I dont know how) the state trigger, because in the visual editor the only state options for that sensor are “unavailable” and “unknown”.
I thought that you guys may not like my implementation of the “wait for trigger” xD it was not that important for me at the moment. But many thanks for the mentioned (better) methods :slight_smile:
The only problem with your automation would be the < 10 state condition because the washing mashine could be drawing <10W in that very moment but be in a program anyways.

You’re welcome! If the state of the sensor is not available or unknown, you could try using a different sensor or entity that reports the power consumption of the washing machine.

Regarding the issue with the below: 10 condition, you’re right that it could potentially turn off the washing machine even if it’s in the middle of a cycle. One way to avoid this is to check the state of the washing machine itself, rather than just the power consumption. For example, if your washing machine has a status sensor that reports whether it’s currently running or not, you could add an additional condition to check that the washing machine is not running before turning it off.

1 Like

Sadly there is no other sensor that I could use. My washing mashine isnt smart at all, I only have the output data of my smart plug to work with.

Im aware that the time thing needs to be redone, but currently i would like to focus on the main problem. Any suggestions about that?:slight_smile:

Thank you for replying.
In the State condition section, I cant configure a state other than “Unknown” or “Unavailable” for “sensor.tasmota_waschmaschine_energy_power”. And with the template or numeric state conditions I cant configure a “for” time :frowning:

Finally I came up with this solution (havent tested it fully yet, but at least it should work in theorie).

Be aware that I used 3 helpers: “counter.waschmaschine_zaehler”, “input_boolean.waschmaschine_on” and “input_datetime.waesche_zeit”. The last one is just for displaying the running time of the washing mashine in the notification.

The automation:

alias: Waschmaschine
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - if:
      - condition: template
        value_template: "{{states(\"sensor.tasmota_waschmaschine_energy_power\")|int <= 4}}"
    then:
      - if:
          - condition: numeric_state
            entity_id: counter.waschmaschine_zaehler
            below: 6
        then:
          - service: counter.increment
            data: {}
            target:
              entity_id: counter.waschmaschine_zaehler
        else:
          - if:
              - condition: state
                entity_id: input_boolean.waschmaschine_on
                state: "on"
            then:
              - service: notify.mobile_app_sm_g973f
                data:
                  title: Waschvorgang beendet!
                  message: |-
                    Laufzeit: {{'\n'}} {{
                      (((states("sensor.timestamp") |as_timestamp|timestamp_custom('%-H'))|int)*60 + (states("sensor.timestamp") |as_timestamp|timestamp_custom('%M'))|int) -
                      (((states("input_datetime.waesche_zeit") |as_timestamp|timestamp_custom('%-H'))|int)*60 + (states("input_datetime.waesche_zeit") |as_timestamp|timestamp_custom('%M'))|int)
                      }} min
                  data:
                    notification_icon: mdi:washing-machine-off
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.waschmaschine_on
          - if:
              - condition: time
                after: "20:00:00"
                before: "07:00:00"
            then:
              - type: turn_off
                device_id: c3fa52a362f4a3a280bc5a373cd95d91
                entity_id: switch.tasmota_waschmaschine
                domain: switch
            else:
              - type: turn_on
                device_id: c3fa52a362f4a3a280bc5a373cd95d91
                entity_id: switch.tasmota_waschmaschine
                domain: switch
          - service: counter.reset
            data: {}
            target:
              entity_id: counter.waschmaschine_zaehler
    else:
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.waschmaschine_on
      - service: input_datetime.set_datetime
        data_template:
          datetime: "{{ states(\"sensor.timestamp\") |as_datetime }}"
        target:
          entity_id: input_datetime.waesche_zeit
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.waschmaschine_zaehler
mode: single

Thanks for helping me :slight_smile: