Timed Switch - Turn Off after time in Slider

Hi there, not sure if has been asked before, and I’ve been reading alot of the other examples. I’m just a rookie, only started to implement Home Assistant with my MQTT and Arduinos recently.
I’m currently controlling my Heating/Boiler with this and what I would like to achieve would be, to have a slider to pick the time the boiler will run then switched on.

Everything is working, except the fact that by some reason the automation is not triggering the switch that will create the delay and then turn off the boiler.
The switch does work if I turn it on manually. I’ve read a lot of other examples and can’t seem to find whats wrong :frowning:

Any help/hint will be greatly appreciated.

Automations

  alias: Run Boiler for X time...
  trigger:
    platform: state
    entity_id: switch.timed_boiler
    to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.boiler_time
      
Scripts

boiler_timer:
  alias: "Boiler Timer"
  sequence:
   - service: notify.notify
      data:
        message: 'Turned on the timer'
    - delay: '00:{{ states.input_number.boiler_run_time.state | int }}:00'
    - service: homeassistant.turn_off
      data:
        entity_id: switch.timed_boiler
    

Slider:

input_number:
  boiler_run_time:
    name: Boiler Time
    initial: 60
    min: 1
    max: 120
    step: 1

Switch:

- platform: command_line
  switches:
    timed_boiler:
      command_on: "/usr/bin/curl -X GET http://192.168.193.101/?button1on"
      command_off: "/usr/bin/curl -X GET http://192.168.193.101/?button1off"
      command_state: "/usr/bin/curl -X GET http://192.168.193.101/?button1status"
      value_template: >-
        {%- if value_json["response"]["values"][0] == 1 -%}
        {{ true }}
        {%- else -%}
        {{ false }}
        {%- endif -%}
      friendly_name: Timed Boiler

May i recommend setting an automation that updates the input_number (slider) every Minute like this:

- alias: update slider boiler_run_time
  hide_entity: True
  trigger:
    platform: time
    seconds: '/60'
  action:
#    - delay: 00:00:01
    - service: input_number.set_value
      data_template:
        entity_id: input_number.boiler_run_time
        value: '{{states.input_number.boiler_run_time.state | int -1}}'
  condition:
    - condition: numeric_state
      entity_id: input_number.boiler_run_time
      above: '0'  

And then a simple automation to turn off the device once the state of the slider is 0.
In that way even if restarting you can keep the timer active and you can see how much time is still on the timer.

Thanks for the reply.

Something like:

- alias: update slider boiler_run_time
  hide_entity: True
  trigger:
    platform: time
    seconds: '/60'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.boiler_run_time
        value: '{{states.input_number.boiler_run_time.state | int -1}}'
  condition:
    - condition: numeric_state
      entity_id: input_number.boiler_run_time
      above: '0' 
- alias: turn off boiler when timmer zero
  hide_entity: True
  trigger:
    platform: numeric_state
    entity_id: input_number.boiler_run_time
    at: '0'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.timed_boiler

? Thanks

To add to this, but this means the counter is always running… How do I only start is when the the boiler is on?

Thanks

This is what I’m testing with now… but I might change the update slider to use the state change of the switch.timed_boiler instead…

- alias: update slider boiler_run_time
  hide_entity: true
  trigger:
    platform: time
    seconds: /60
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.boiler_run_time
      value: '{{states.input_number.boiler_run_time.state | int -1}}'
  condition:
  - condition: numeric_state
    entity_id: input_number.boiler_run_time
    above: '0'
  id: 6eb541462f77409f949553a73ce8c6cb
- id: '1547128786392'
  alias: turn off boiler when timmer zero
  trigger:
  - platform: time
    seconds: /60
  condition:
  - below: '1'
    condition: numeric_state
    entity_id: input_number.boiler_run_time
  action:
  - entity_id: switch.timed_boiler
    service: homeassistant.turn_off
  hide_entity: true

In that way even if restarting you can keep the timer active and you can see how much time is still on the timer.

But if I restart HA the slider goes back to the default which is 60…

input_number:
boiler_run_time:
name: Boiler Time
initial: 60
min: 0
max: 120
step: 1

Final working version… apologies for the spam…

- id: 6eb541462f77409f949553a73ce8c6cb
  alias: update slider boiler_run_time
  trigger:
  - entity_id: switch.timed_boiler
    platform: state
    to: 'on'
  condition:
  - above: '0'
    condition: numeric_state
    entity_id: input_number.boiler_run_time
  action:
  - data_template:
      entity_id: input_number.boiler_run_time
      value: '{{states.input_number.boiler_run_time.state | int -1}}'
    service: input_number.set_value
  hide_entity: true
- id: '1547128786392'
  alias: turn off boiler when timmer zero
  trigger:
  - below: '1.0'
    entity_id: automation.update_slider_boiler_run_time
    platform: numeric_state
  condition: []
  action:
  - entity_id: switch.timed_boiler
    service: homeassistant.turn_off
  hide_entity: true

Now need to understand what you meant by, after restart will keep going… :confused:

1 Like

Ah sorry, of course you need to delete the initial value. In this case the restart will keep the value it was before :slight_smile:

Care to elaborate? I’m still a rookie so not really sure if I understood :slight_smile:

Thanks for the help

If you use an initial value it will override the value that was last set before a restart.

If you set an initial_value it will always use that value when HA starts, hence the name.

If you don’t set an initial_value it will either show the last set value before a restart, or it will be unknown - the latter could happen for two reasons:

  1. it’s the first time you start HA with this automation at all
    or
  2. the value that was supposed to be stored has been deleted, e.g. by deleting the complete database, or another error (I restart HA from the command line of my Pi with sudo systemctl restart home-assistant@homeassistant and I have managed to do this a second time while the system had no fully started up the first time, deleting all my input_whatever values).
1 Like

Thanks for the great clarification :slight_smile: