Tv sleep timer

Hi I’m trying to do a very simple input select sleep timer for the tv

In configuration

input_select:
  tv_sleep_timer:
    name: tv_sleep_timer
    options:
    - 30 minutes
    - 1 hour
    - 2 hours
    initial: 1 hour

timer:
  samsung_tv30:
    duration: '00:30:00'
  samsung_tv1:
    duration: '01:00:00'
  samsung_tv2:
    duration: '02:00:00'


scripts

'tv_sleep_timer':
  alias: Tv sleep timer
  sequence:
  - service: timer.start
    data_template:
      entity_id: >
         {% if is_state("input_select.tv_sleep_timer", "30 minutes") %} timer.samsung_tv30
         {% elif is_state("input_select.tv_sleep_timer", "1 hour") %} timer.samsung_tv1
         {% elif is_state("input_select.tv_sleep_timer", "2 hours") %} timer.samsung_tv2
         {% endif %}


and automation

- id: 'tv sleep off'
  alias: Tv sleep off
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.samsung_tv30
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.samsung_tv1
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.samsung_tv2
  action:
    service: media_player.turn_off
    entity_id: media_player.samsung_tv_remote

I’ve entered the entity into lovelace

but the timers wont trigger?

I can’t see where the error is and I’m sure it must be very obvious?

Thanks

Scripts don’t do anything by themselves. They need to be executed either manually or in an automation. This automation would trigger whenever the input_select changes and should start the appropriate timer:

- alias: Sleep timer
  initial_state: true
  trigger:
  - platform: state
    entity_id: input_select.tv_sleep_timer
  action:
  - service: timer.start
    data_template:
      entity_id: >-
        {% set input = trigger.to_state.state %}
        timer.samsung_tv{{ input[:input.find(' ')] }}

You won’t need that script with this.

Thank you, I seem to have a bit of block in my ability to understand the differences between scripts and automations.

Marked as a solution.

If it helps, the action of an automation and a script are basically the same thing.

I think that does help… the concepts of the 2 are fairly close, I just struggle with working out which one is being triggered against which one is a flow of event’s.

The two things to me seem very similar - :slight_smile: hence my mistake above…

An automation consists of three parts: one or more triggers (events), optional condition(s), and an action. When a trigger happens, and there are no conditions or the conditions are all true, the action is executed.

A script only consists of one part: an action. It’s not looking for an event (trigger) to happen like an automation is.

Adding to what Tediore said.
Putting stuuf in a script, means that timimg events don’t get confused and automations can still be called.
Running an automation the second time whilst a timer in that automation is running just skips stuff straight to the action. (you have to make sure to cancel the script before calling it again if that is the issue).
Scripts also allow you to write one set of code and call it from multiple automations
Scripts can be called if you want to ‘set a scene’, I don’t use scenes, I just run a script.