Automation not trigering

Hello,

I try to shut down a printer after it’s state is idle for 10 minutes.

Therefore I do have an automation build:

alias: Printer uitschakelen
description: ""
trigger:
  - platform: template
    value_template: |-
      (as_timestamp(now())) -
           (as_timestamp(states.sensor.cnmf633c_635c.last_changed)) | float > 600
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.schakelaar_printer
mode: single

In template tester the trigger value is true (so the last change is more than 600 seconds), and the ‘switch.schakelaar_printer’ shows idle (see screenshot).

I would suppose this is correct, but the automation does not trigger at all.
Manualy triggerd it does turn off the printer-switch.

What do I do wrong?

first off you are not enclosing your template in brackets ( {{ … }} )

Next, I would change the multi-line character to the standard one used in HA.

lastly, are you waiting for the trigger to change state from false to true?

if the sensor last_changed value is already greater than 600 seconds ago it won’t trigger.

you need to wait till the sensor last_changed goes from less than 600 seconds to more than 600 seconds ago.

trigger:
  - platform: template
    value_template: >
      {{ (as_timestamp(now())) - (as_timestamp(states.sensor.cnmf633c_635c.last_changed)) | float > 600 }}

The template has to change from false to true to trigger. If it is already true when you create the automation it won’t trigger.

Also this can be done a lot simpler.

trigger:
  - platform: state
    entity_id: sensor.cnmf633c_635c
    to: idle
    for: 600
1 Like

OK, thanks!

So these templates seem to be not exactly what I want.
I want to test if the sensor is 600 seconds or longer ‘idle’.

Is that possible in HA?

use @tom_l’s suggestion above. no templates required and it should do exactly what you say you want to do.

I tried, but unfortunately it does not do what I hoped.

If for one or else reason within those 600 seconds HA restarts for one or else reason, the trigger fails. After a restart of HA the printer is in idle state for more than 10 minutes, but the automation does not trigger. I am looking for a kind of trigger ‘for more than’ in stead of ‘for’.

As Tom a;ready said: when HA starts and the printer is in idle state, the automation won’t trigger.

It does exactly what you originally asked for. Then you added another requirement involving a restart. No trigger survives a restart. In fact, neither does the value of a sensor’s last_changed property. On startup, it’s set to the current time and date.

You may wish to consider creating a Trigger-based Template Sensor that uses a State Trigger to monitor the printer’s state-changes (from/to idle) and reports the time difference (in seconds). Your automation can use a Template Trigger to detect when the value of the Trigger-based Template Sensor exceeds 600 seconds.

What you need is another trigger and a condition.

So the automation triggers if the device has been idle for 600 seconds (first state trigger), or if the automation has been reloaded (your second trigger), then make sure (with a state condition) the device has been idle for 600 seconds.

I fixed the puzzle in a somewhat other way.

I created a timer and an automation. If the timer get idle, the printer goes off.

Anyhow: thanks for helping!

alias: Printer timer
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cnmf633c_635c
    to: idle
  - platform: homeassistant
    event: start
condition: []
action:
  - if:
      - condition: state
        entity_id: sensor.cnmf633c_635c
        state: idle
    then:
      - service: timer.start
        data:
          duration: "00:10:00"
        target:
          entity_id: timer.printer
mode: single

Yeah but if your sensor changes from idle to something else the timer is not stopped. Do it the way I suggested. You don’t need a timer.

Works great.
Tahnks!!

At startup, won’t the sensor’s last_changed be set to the startup time?

So when the automation is triggered at startup, the condition that checks if the sensor has been idle for 600 seconds can’t be true. Yet brinkgit has concluded this “Works great”.

What have I misunderstood here?

First 600 seconds the firat trigger reacts.
If HA restarts it checks if printer is still idle. If so: it shuts down.

Not exactly what I had in mind, but good enough for me…

Now I understand. You eliminated one of your original requirements.