Data_template works with entity_id, but no way to make it work with service

The only way to solve it, unless anyone has a better understanding, was the one that follows but I really don’t like repeating code 3 times… there should be something like a case statement or so…shouldn’t it? Thanks in advance :wink:

t_on:
  alias: t_on
  sequence:
  - service: switch.turn_on
    data_template: 
      entity_id: >
        {% if (states.input_select.timer_device.state=='Oven') %}
        switch.sonoff_t1_b2
        {% elif (states.input_select.timer_device.state=='Fan') %}
        switch.sonoff06
        {% elif (states.input_select.timer_device.state=='Pool') %}
        switch.sonoff02
        {% endif %}

t_off:
  alias: t_off
  sequence:
  - service: switch.turn_off
    data_template: 
      entity_id: >
        {% if (states.input_select.timer_device.state=='Oven') %}
        switch.sonoff_t1_b2
        {% elif (states.input_select.timer_device.state=='Fan') %}
        switch.sonoff06
        {% elif (states.input_select.timer_device.state=='Pool') %}
        switch.sonoff02
        {% endif %}

t_toggle:
  alias: t_toggle
  sequence:
  - service: switch.toggle
    data_template: 
      entity_id: >
        {% if (states.input_select.timer_device.state=='Oven') %}
        switch.sonoff_t1_b2
        {% elif (states.input_select.timer_device.state=='Fan') %}
        switch.sonoff06
        {% elif (states.input_select.timer_device.state=='Pool') %}
        switch.sonoff02
        {% endif %}

exec_timer2:
  alias: Set device without timer
  sequence:
  - service: script.turn_on
    data_template: 
      entity_id: >
        {% if (states.input_select.on_off_toggle.state=='On') %}
        script.t_on
        {% elif (states.input_select.on_off_toggle.state=='Off') %}
        script.t_off
        {% elif (states.input_select.on_off_toggle.state=='Toggle') %}
        script.t_toggle
        {% endif %}

Sure. Use service_template.

Thanks so much. I’ll work it out and post the resulting code.

1 Like

Solved!

Here goes the code, quite shorter :slight_smile: :

exec_timer2:
  alias: Set device without timer
  sequence:
  - service_template: >
      {% if (states.input_select.on_off_toggle.state=='On') %}
      switch.turn_on
      {% elif (states.input_select.on_off_toggle.state=='Off') %}
      switch.turn_off
      {% elif (states.input_select.on_off_toggle.state=='Toggle') %}
      switch.toggle
      {% endif %}
    data_template: 
      entity_id: >
        {% if (states.input_select.timer_device.state=='Oven') %}
        switch.sonoff_t1_b2
        {% elif (states.input_select.timer_device.state=='Fan') %}
        switch.sonoff06
        {% elif (states.input_select.timer_device.state=='Pool') %}
        switch.sonoff02
        {% elif (states.input_select.timer_device.state=='Beep') %}
        script.beep
        {% endif %}

You can make it even shorter with:

  - service_template: >
      {% if is_state('input_select.on_off_toggle','Toggle') %}
        switch.toggle
      {% else %}
        switch.turn_{{ states('input_select.on_off_toggle')|lower }}
      {% endif %}

:slight_smile:

BTW, your last entity_id won’t work here. You can’t start a script with a switch service. You’d have to adjust the service_template so that if input_select.timer_device is ‘Beep’, then it calls the script.turn_on service.

@polclota could just swap all the services with homeassistant.turn_on, homeassistant.turn_ff, and home assistant.toggle.

EDIT: not sure how the script will behave…

1 Like

Lovely… the shorter the better :ok_hand:

I see, you’re both write, it was just for testing purposes…and indeed, it did not work.Now it works fine. Thank you all!

exec_timer2:
  alias: Set device without timer
  sequence:
  - service_template: >
      {% if is_state('input_select.on_off_toggle','Toggle') %}
        homeassistant.toggle
      {% else %}
        homeassistant.turn_{{ states('input_select.on_off_toggle')|lower }}
      {% endif %}
    data_template: 
      entity_id: >
        {% if (states.input_select.timer_device.state=='Oven') %}
        switch.sonoff_t1_b2
        {% elif (states.input_select.timer_device.state=='Fan') %}
        switch.sonoff06
        {% elif (states.input_select.timer_device.state=='Pool') %}
        switch.sonoff02
        {% elif (states.input_select.timer_device.state=='Beep') %}
        script.beep
        {% endif %}
2 Likes

In general, when you’re testing an entity state, it’s better to write it this way:

is_state('input_select.timer_device', 'Oven')

Not only is it shorter, it won’t cause an error if the entity doesn’t exist yet.

1 Like

Sure, I changed it and it works really fine! Thanks!
Next follows final code in case it might help other users. :ok_hand:

exec_timer2:
  alias: Set device without timer
  sequence:
  - service_template: >
      {% if is_state('input_select.on_off_toggle','Toggle') %}
        homeassistant.toggle
      {% else %}
        homeassistant.turn_{{ states('input_select.on_off_toggle')|lower }}
      {% endif %}
    data_template: 
      entity_id: >
        {% if is_state('input_select.timer_device', 'Oven') %}
        switch.sonoff_t1_b2
        {% elif is_state('input_select.timer_device', 'Fan') %}
        switch.sonoff06
        {% elif is_state('input_select.timer_device', 'Pool') %}
        switch.sonoff02
        {% elif is_state('input_select.timer_device', 'Beep') %}
        script.beep
        {% endif %}