Automatic turn off switch after X minutes not working anymore

Hi!

I have an automation which turns off my coffee machine (just a wall plug really) after X minutes. It has been working great until I split my config to separate yaml-files and upgraded to HASS 0.55.1. Now it seems that it doesn’t care about the timer any longer. If I set the timer to 1 minute, nothing happens after 1 minute. The switch stays on and there’s nothing in the log after 1 minute.

Here is the code that I’m using: https://hastebin.com/vutafovuro.coffeescript

Can someone see the problem? Or do you know a better way of accomplishing this? I want to have a slider for the minutes so I can manually choose a longer or shorter period according to the situation.

I’d suggest using a template binary sensor like this:

binary_sensor:
  - platform: template
    sensors: 
      kaffebryggare_kvar_pa:
        friendly_name: 'Kaffebryggare: kvar på'
        value_template: >-
          {% if is_state("switch.kaffebryggare_switch", "on" ) %} 
            {{ ( as_timestamp(now()) - as_timestamp(states.switch.kaffebryggare_switch.last_changed)|float ) > ( states.input_number.kaffebryggare_run_time.state|float * 60 ) }}
          {% else %}
            False
          {% endif %}

And an automation that uses it:

automation:
  alias: "Kaffebryggaren: Automatisk avstängning"
  trigger:
    - platform: state
      entity_id: binary_sensor.kaffebryggare_kvar_pa
      to: 'on'
  action:
    - service: switch.turn_off
      entity_id: switch.kaffebryggare_switch
2 Likes

Haha hi Tink! I thought to myself “I wonder if he’ll see this and think that I’m stupid for not using his solution” :wink: I guess I should try to use your solution. It’s maybe the best way to do this.

:smiley:

I used to do it your way, until the devs flagged up that delay holds a thread open on Home Assistant. Short delays of a few seconds are no big deal, but the longer it goes on for, the more of an impact it has.

Yeah, sounds reasonable. Better to do it the right way! I will give it another go tonight, thanks for all the help :smiley:

@Tinkerer - I got it working last night with your solution! Big thanks :smiley: