Automation trigger of state change after being at a state for a time

I’m pretty sure that’s not correct.

I’m pretty sure phil was the one who created the PR to implement the for: option for a template trigger.

it’s in the docs here: Automation Trigger - Home Assistant

I’m sure I’ve tried to do that in the past and it wouldn’t let me… Well hell, that certainly solves many problems.

How old is the ability to do that?

1 Like

I hope it’s newer than May 13th or this whole thread was for nothing :slight_smile:

automation:
  trigger:
    platform: template
    value_template: "{{ (as_timestamp(states.sensor.time.last_changed) - as_timestamp(states.YOUR.ENTITY.last_changed)) > 300 }}"
automation:
  trigger:
    platform: template
    value_template: "{{ is_state('device_tracker.paulus', 'home') }}"
    for:
      minutes: "{{ states('input_number.minutes')|int(0) }}"

This definitely works, but it will only fire when sensor.time updates: on the minute. This means that your automation could end up firing up to 59 seconds later than the equivalent state trigger with for.

For the second example, what happens if input_number.minutes is changed after the device tracker’s state changes, but before the automation is triggered?

Sorry that not mine its just examples in the docs
Template triggers

I suck at HA yaml automation :joy:
I use Node Red

@SteveDinn
I will work on a Node Red flow and post it later

@SteveDinn
Okay here is a simple Node-Red
Basically the logic is

State Change Node
Outputs Only On State Change
is True Send to Loop
is False send to Change Node

Change Node
Changes Payload to “Stopped”

Loop Timer Node
Output During Loop every 10 seconds
Outputs On:
120 Loops Max Loop Reached
Or
25 Minutes Max Time (to stop infinite loop)
Or
It Receives a Stop Message

Switch Node
Only Outputs on Payload that contains “max loop reached”

Call Service Node
Do Something

[{"id":"a4b4c4bf.e4a75","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"b59fb79c.665058","type":"server-state-changed","z":"a4b4c4bf.e4a75","name":"","server":"ef392d83.86c5a","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.washing_machine_on","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":190,"y":80,"wires":[["7311aa40.262554"],["10f05777.57fae9"]]},{"id":"7311aa40.262554","type":"looptimer","z":"a4b4c4bf.e4a75","duration":"10","units":"Second","maxloops":"120","maxtimeout":"25","maxtimeoutunits":"Minute","name":"","x":500,"y":80,"wires":[[],["fec82210.c9d7c"]]},{"id":"10f05777.57fae9","type":"change","z":"a4b4c4bf.e4a75","name":"Send Stop","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":160,"wires":[["7311aa40.262554"]]},{"id":"fec82210.c9d7c","type":"switch","z":"a4b4c4bf.e4a75","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"max loops reached","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":670,"y":80,"wires":[["3a94c329.3881e4"]]},{"id":"3a94c329.3881e4","type":"api-call-service","z":"a4b4c4bf.e4a75","name":"","server":"ef392d83.86c5a","version":1,"debugenabled":false,"service_
 domain":"climate","service":"turn_off","entityId":"climate.daikin_skyzone","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":860,"y":80,"wires":[[]]},{"id":"ef392d83.86c5a","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

When was the for option added to the template trigger? I added basic support in PR #24330, then added support for using templates with the option in PR #24810. Both were submitted in June. The first made the 0.95 release and the second made the 0.96 release.

Ha ha…about one month after I went looking for it. Good add :slight_smile: This prevents me having to create timers and/or otherwise useless sensors.

There are times when I would prefer that everything was just a template.

Per the documentation:

The for template(s) will be evaluated when the value_template becomes true .

So the answer is nothing.

I will like to get a trigger where the attribute value change state from a specific value to a complete different random value.
Good for Thoroughfare attribute on geocoded_location sensors.

The only solution is then the state changed with this trigger:

- platform: state
    entity_id: sensor.newipone_geocoded_location

You can do this check with a condition:

trigger:
  - platform: state
    entity_id: sensor.newipone_geocoded_location
variables:
  your_attribute: 'YOUR_ATTRIBUTE_NAME'
  special_value: 'YOUR_SPECIAL_VALUE'
condition:
  - condition: template
    value_template: "{{ trigger.from_state.attributes[your_attribute] == special_value }}"
  - condition: template
    value_template: "{{ trigger.to_state.attributes[your_attribute] != special_value }}"
1 Like

This thread was exactly what I needed. Thank you all!!!

I have a Sony television that, when on, will flip-flop its state—a consistent 15 seconds ‘on’ followed by 10 seconds ‘off’. Repeating this over and over. When it is off, it properly reports the state as ‘off’. Thanks to the ideas here, I created a timed delay to confirm the state was really ‘off’ before taking action. When the state changes to ‘off’, it waits until a flip-flop would make it ‘on’ and checks if it is really ‘off’. It causes a delayed reaction, but it’s better than seeing a button constantly change state. I wanted to share that with others who may have this same issue. I am sure there is a more elegant way to do this, but here you go.

Thank you!!

—————

Helper:
Input_boolean called input_boolean.main_tv_state

Button code:

          - type: custom:button-card
            entity: input_boolean.main_tv_state
            name: Tv
            tap_action:
              action: call-service
              service: media_player.turn_off
              service_data:
                entity_id: media_player.main_sony_tv
              target:
                entity_id: media_player.main_sony_tv

Automation Trigger:


alias: Main TV - Off
description: Check if Main TV off for 16 seconds in a row.
trigger:
  - platform: state
    entity_id:
      - media_player.main_sony_tv
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.main_tv_state
  - delay:
      hours: 0
      minutes: 0
      seconds: 16
      milliseconds: 0
  - if:
      - condition: state
        entity_id: media_player.main_sony_tv
        state: "off"
    then:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.main_tv_state
mode: single

1 Like