Turn switch on then off after 30 mins

I have a towel rail controlled by a Shelly switch which works fine in automation mode and I can also switch it on manually.
What I am trying to do is to have a button on a card where I can manually turn it on and have it turn off automatically after 30 minutes.
I have a script for this which I call from a button but its not working so I am looking for good advice. This is my code:

  alias: Towel rail adhoc run
  sequence:
    - type: turn_on
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch
    - type: turn_off
    - delay: "00:30:00"
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch

Can someone please help me out and show me what I am doing wrong?
Thanks

Could you not do it via automation?

alias: Towel rail adhoc run
mode: single
trigger:
  - platform: state
    entity_id:
      - button.name of your button
    to: "on"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.shelly1pm_e89f6d8672dd
  - delay:
      minutes: 30
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.shelly1pm_e89f6d8672dd

Yes possibly but I am not sure how to do the trigger, specifically the name of the button.
When I create the button, I have to assign an entity and a name.
So I named the button ‘towel_rail_adhoc’. But that name is not recognised when I try it in the automation.yaml. I get this:

 trigger:
    - platform: state
      entity_id:
        - towel_rail_adhoc   String does not match pattern of "^(?!.__)(?!_)[\da-z_]+(?<!_)\.(?!_)[\da-z_]+(?<!_)$"

Any ideas?
Thanks for your help BTW. Much appreciated.

This yaml is not right:

It should be:

  alias: Towel rail adhoc run
  sequence:
    - type: turn_on
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch
    - delay: "00:30:00"
    - type: turn_off 
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch
1 Like

Hi Ross,
I tried that it still doesn’t work. I assign the script to a button and it says ‘unavailable’.

The code for the button:

show_icon: false
type: button
tap_action:
  action: toggle
entity: script.towel_rail_adhoc_run
name: Towel rail manual
show_state: true

Ok assuming you have corrected the script, have you reloaded scripts? and have you confirmed the script runs correctly first?

Also to reduce any confusion with buttons etc, this statement is not true, if you are referencing to creating a button in dashboard:

You can create a front end button without any reference to an actual entity by using the ‘tap’ and or ‘hold’ options like this for example: (note: no entity listed)

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: script.turn_on
  data: {}
  target:
    entity_id: script.test_script
show_state: true
name: test
hold_action:
  action: none

Yes I corrected the script but can you please do a sanity check for me:

Towel rail adhoc run:
  alias: Towel rail adhoc run
  sequence:
    - type: turn_on
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch
    - delay: "00:30:00"
    - type: turn_off
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch

I reloaded the script. It does not run, I get a message flashing up:
image

I just clicked ‘Show info for script’ and I get this:
image
Not sure why. Perhaps I should Remove the script and start again.

There is nothing wrong with the above script yaml, but clearly something wrong with the script itself.

Yes, good idea!

I assume this is cut and pasted from your scripts.yaml?

If so you should not use CAPITALS and you cant use spaces, you need to replace any spaces with _ like this:

towel_rail_adhoc_run:
  alias: Towel rail adhoc run

Yes copied from scripts.yaml. I have fixed what you pointed out:

towel_rail_adhoc_run:
  alias: Towel rail adhoc run
  sequence:
    - type: turn_on
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch
    - delay: "00:30:00"
    - type: turn_off
      device_id: 204af44613e4dbe8000502b8d724272f
      entity_id: switch.shelly1pm_e89f6d8672dd
      domain: switch

I tried what you mentioned about the button, that works. Thanks.
The script works now.
I have lost count of how many times I have reloaded the scripts.
I did not have ‘script: !include scripts.yaml’ in configuration.yaml. I added that and it seems to work OK now but in doing so, I seem to have created a new problem; I get error messages from some of my automations. Perhaps a problem for tomorrow.

One small ‘thing’; the button does not show the state of the device/entity.
Code for the button:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: switch.toggle
  data: {}
  target:
    device_id: 204af44613e4dbe8000502b8d724272f
    entity_id: switch.shelly1pm_e89f6d8672dd
show_state: true
name: Towel rail
hold_action:
  action: none
icon_height: 40px
icon: mdi:lightbulb

Any thoughts?

With the above button yaml as it is, it wont show a state as there is no entity listed, you are simply using a button (with no entity) and using that button to call a script in to action - so there is no entity to show the state of.

If you you want to show the state, then you need to revert back to putting the entity (either the script or the switch - as you keep changing the way you are doing it looking at the above!) in the button card and use the toggle option instead of calling a service.

Note however that if you reference the switch it will clearly show that state (‘on’ or ‘off’) however if you reference the script it will show the state when you toggle it but literally only for a split second while the script is actually running.

Hope that makes sense.

Yes that makes sense. Thanks very much for your help.

Think it may be better to mark one of my post as the solution !

Absolutely right! I actually thought that’s what I did, I did not mean to be a smart arz. Fixed.
Thanks again.

1 Like

to help you understand this problem from earlier.

trigger:
    - platform: state
      entity_id:
        - towel_rail_adhoc

You needed to add button. in front of the entity. i.e button.towel_rail_adhoc

Thanks, appreciate it.

Relying on delays like that in automations might not be the best idea. Simply saving a new automation while that’s running well cancel it. Restarting HA will cancel it.

Imo when delays are over a few seconds or so, you should use timers or input_datetimes to make them restart safe.