Suggestions on how to turn of a device after certain time without using delay

Hi,

How can I simplyfy and make these automations better?

The first one I could make a look to set the value on the input datetimes instead of calling each service in a separate block each time? Or just not use the input datetimes at all?

alias: VVB Strøm saver
description: ''
trigger:
  - platform: time
    at: '23:00'
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[0].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime1
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[1].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime2
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[2].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime3
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[3].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime4
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[4].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime5
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[5].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime6
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[6].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime7
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[7].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime8
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[8].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime9
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[9].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime10
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[10].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime11
  - service: input_datetime.set_datetime
    data:
      timestamp: >-
        {{
        states('sensor.billige_timer_imorgen').split(',')[11].replace('[','').replace(']','')
        }}
    target:
      entity_id: input_datetime.billigtime12
  - service: notify.mobile_app_thomas_mobil
    data:
      message: Billige timer satt
mode: single

Which sets a series of input_datetime helpers from a unix timestamp array which can look like this:

[1644534000, 1644537600, 1644541200, 1644544800, 1644548400, 1644552000, 1644577200, 1644580800, 1644588000, 1644609600, 1644613200, 1644616800]

These datetimes are used as triggers in another automation which looks like this:

alias: VVB Av etter en time
description: ''
trigger:
  - platform: time
    at: input_datetime.billigtime1
  - platform: time
    at: input_datetime.billigtime2
  - platform: time
    at: input_datetime.billigtime3
  - platform: time
    at: input_datetime.billigtime4
  - platform: time
    at: input_datetime.billigtime5
  - platform: time
    at: input_datetime.billigtime6
  - platform: time
    at: input_datetime.billigtime7
  - platform: time
    at: input_datetime.billigtime8
  - platform: time
    at: input_datetime.billigtime9
  - platform: time
    at: input_datetime.billigtime10
  - platform: time
    at: input_datetime.billigtime11
  - platform: time
    at: input_datetime.billigtime12
condition: []
action:
  - type: turn_on
    device_id: c4d85a15f1e26ff043d4b628caf30968
    entity_id: switch.varmtvannstank
    domain: switch
  - service: notify.mobile_app_thomas_mobil
    data:
      title: 'VVB PÅ Billig '
      message: '{{now()}}'
  - delay:
      hours: 0
      minutes: 59
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: c4d85a15f1e26ff043d4b628caf30968
    entity_id: switch.varmtvannstank
    domain: switch
  - service: notify.mobile_app_thomas_mobil
    data:
      title: VVB AV 59min
      message: '{{now()}}'
mode: single

Is there a better way to turn the device on again after 59 minutes, instead of using delay? If I restart HA during one of these hours the automation will not work as intended I guess? Another set of datetime helpers could be used where I just add 59 min to the Unix timestamp, but dealing with 24 helpers isnt ideal I guess

I assume you mean ‘turn off’ after a delay?

Your first action turns it on.

If so, use two automations:

Turn on

alias: VVB Av etter en time
description: ''
trigger:
  - platform: time
    at: input_datetime.billigtime1
  - platform: time
    at: input_datetime.billigtime2
  - platform: time
    at: input_datetime.billigtime3
  - platform: time
    at: input_datetime.billigtime4
  - platform: time
    at: input_datetime.billigtime5
  - platform: time
    at: input_datetime.billigtime6
  - platform: time
    at: input_datetime.billigtime7
  - platform: time
    at: input_datetime.billigtime8
  - platform: time
    at: input_datetime.billigtime9
  - platform: time
    at: input_datetime.billigtime10
  - platform: time
    at: input_datetime.billigtime11
  - platform: time
    at: input_datetime.billigtime12
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.varmtvannstank
  - service: notify.mobile_app_thomas_mobil
    data:
      title: 'VVB PÅ Billig '
      message: '{{now()}}'
mode: single

Turn off:

alias: Turn off after 59 minutes
trigger:
  - platform: state
    entity_id: switch.varmtvannstank
    to: 'on'
    for:
      minutes: 59
action:
  - service: switch.turn_off
    target:
      entity_id: switch.varmtvannstank
  - service: notify.mobile_app_thomas_mobil
    data:
      title: VVB AV 59min
      message: '{{now()}}'
mode: single

This trigger off for time will be reset and start counting again if automations are reloaded or when Home Assistant restarts.

If the time is super critical you could start a timer in the “turn on” automation and use the timer finishing as the trigger for your off automation. The timer will not be affected by automation reloads and there is a method to restore active timers after a restart, see: https://community.home-assistant.io/t/restore-active-paused-timers-after-a-restart/274439

Hi,

Yes, sorry typo. Turn OFF is correct.

It is “critical” that the device is turned off again within the same hour. The documenation for timer says

With the current implementation timers don’t persist over restarts. After a restart, they will be idle again, together with their initial configuration.

I am not concerned with automation reloads, but rather system restarts.

Edit: using time pattern 59 min should do the trick, making sure device is always turned off at the end of every hour. If the device is already off it wont matter?

Question for the service input_datetime.set_datetime, this is doable in a loop as well, using the index in the loop to set the right timestamp to the right helper?

Edit: Another question: If the helper nr 12 is at 23:00, and the automation starts, with the delay 59min. If I in another automation change the helper-value that shouldnt affect the already runnin delay? I saw some unexpected behaviour, that the device was never turned off after 23:00, value of last helper.