Service_template & data_template within 1 automation

I’m trying to make an automation to turn on a light with a certain brightness and color, and with the same automation turn it off (without the extra data, otherwise you get errors)

I thought of using a service_template, that works if I don’t use the data_template. The config validation gives “configuration correct”, but triggering gives an error:

ERROR (MainThread) [homeassistant.helpers.service] Template rendered invalid service: light.turn_on
data_template: >
entity_id: light.bedroom
brightness: 5
kelvin: 4000

- id: '0000000000004'
  alias: Bedroomswitch left with color
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.wall_switch_left_xxxyyyzzz
      click_type: single
  action:
    service_template: >
     {% if is_state("light.bedroom", "off") -%}
      light.turn_on
     {%- else %}
      light.turn_off
     {%- endif %}
      data_template: >
       entity_id: light.bedroom
       {% if is_state("light.bedroom", "off") -%}
       brightness: 5
       kelvin: 4000
       {%- endif %}
  initial_state: 'on'
1 Like
  1. Your indentation is wrong for data_template. Needs to move over 2 spaces
  2. You cannot populate multiple keys in a single data template. Templates need to be created for each field because templates only return 1 simple string. They do not return complex objects, like json or yaml.
  3. You cannot populate information if the information is not aloud in the service. For example, brightness and kelvin are not valid data properties for the light.turn_off service. So you need to condititionally avoid that service all together.

So if you want to make this work, you’ll either need to separate your automation action into 2 scripts or fire the turn_on automation twice (first turning on, second setting the brightness and kelvin).

- id: '0000000000004'
  alias: Bedroomswitch left with color
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.wall_switch_left_xxxyyyzzz
      click_type: single
  action:
    service_template: "script.bedroom_turn_{{ 'on' if is_state('light.bedroom','off') else 'off' }}"
script:
  bedroom_turn_on:
    sequence: 
      - service: light.turn_on
        data:
          entity_id: light.bedroom
          brightness: 5
          kelvin: 4000
  bedroom_turn_off:
    sequence: 
      - service: light.turn_off
        data:
          entity_id: light.bedroom
2 Likes

Is there also a way that you can say:

service_template: "script.bedroom_turn_{{ 'on' if is_state('light.bedroom','off') else 'off' }}"

not equal to ‘off’ so everthing else then turn on

if is_state(‘light.bedroom’,‘off’)

1 Like

I don’t know what you’re saying. Are you trying to invert the light so that if the light is on, everything else is off and vice versa?

I solved it. Made this script action :slight_smile:

  - service_template: >
      {% if states.input_select.select_holiday.state == "Christmas" %} script.christmas_turn_on
      {% elif states.input_select.select_holiday.state == "Halloween" %} script.halloween_turn_on
      {% elif states.input_select.select_holiday.state == "Kingsday" %} script.kingsday_turn_on
      {% elif states.input_select.select_holiday.state == "Bday" %} script.birthday_turn_on
      {% elif states.input_select.select_holiday.state == "Nothing" %} script.normal_turn_on
      {% endif %} 
1 Like
- id: '0000000000004'
  alias: Bedroomswitch left with color
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.wall_switch_left_xxxyyyzzz
      click_type: single
  action:
    service_template: "script.bedroom_turn_{{ 'on' if is_state('light.bedroom','off') else 'off' }}"

How did you know to use an event trigger as opposed to a state trigger, or could you use either equally effectively? Although the docs explain each, I didn’t get from them when one would be preferred over the other, something you might find in a ‘best practice’ section. I initially considered that you used event because it occurs before the state change, if that is important in this case. But I have no idea which happens first, or if particular entities even fire events, and under which conditions, or if they’re available for use, versus an entity’s state. That information would help me (at least) in building triggers in automations.

Based on the trigger in question.

- platform: event

will produce a event trigger object.

- platform: state

will produce a state trigger object.


So if you have an automation with 2 triggers:

trigger:
- platform: event
  ...
- platform: state
  ...

You have to check which trigger object you are getting during the automation because either could be triggering the automation.

if you just have a single trigger, there’s no reason to check and all you need to do is use the correct trigger object. Which is defined in your trigger section.

That all makes sense, but I’m not sure if that answers the question of how you decided to use an event trigger over a state trigger, since in this case you had only a single trigger. Could you have just as easily used some sort of State as the trigger. Instead of event? Maybe I’m wrong I’m thinking that there are situations where you could use either.

It depends on what you’re trying to do. In the case of the OP, he is using an event because his device supports a single click event. A state trigger is not possible because the device doesn’t report it as a state change.

1 Like

Hello,
i try the code but don’t work:

  - data_template: >
      {{% if state(''input_select.aspi_''+states(''sensor.nameday'')) == "Cuisine" %} script.cuisine
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Salon" %} script.salon
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Couloir" %} script.couloir
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger" %} script.salle_a_manger
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine" %} script.sam_cuisine
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine + Couloir" %} script.sam_cuisine_couloir 
      {% elif state(''input_select.aspi_''+states(''sensor.nameday'')) == "Nettoyage total" %} script.sam_cuisine
      {% endif %}}

i gad this error message:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'state') for dictionary value @ data['condition'][0]['value_template']. Got None
invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['action'][0]['service_template']. Got None. (See /config/configuration.yaml, line 12). 

Help please

that should be {%

states

And the
endif %}}
should just be
endif %}
Too

thanks for your quick answers.
I made the correction but still don’t work:

  action:
  - data_template: >
      {% if states(''input_select.aspi_''+states(''sensor.nameday'')) == "Cuisine" %} script.cuisine
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salon" %} script.salon
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Couloir" %} script.couloir
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger" %} script.salle_a_manger
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine" %} script.sam_cuisine
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine + Couloir" %} script.sam_cuisine_couloir 
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Nettoyage total" %} script.sam_cuisine
      {% endif %} 
  Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None
invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'states') for dictionary value @ data['condition'][0]['value_template']. Got None. (See /config/configuration.yaml, line 12). 

Well, that’s cause your assigning it to the data_template field. I’m assuming you want to use service_template.

thanks.
i change to:

  action:
  - service_template: >

have this error message:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘input_select’) for dictionary value @ data[‘action’][0][‘service_template’]. Got None. (See /config/configuration.yaml, line 12).

with:

  action:
  - service_template: >
      {% if states(''input_select.aspi_''+states(''sensor.nameday'')) == "Cuisine" %} script.cuisine
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salon" %} script.salon
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Couloir" %} script.couloir
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger" %} script.salle_a_manger
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine" %} script.sam_cuisine
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Salle a manger + Cuisine + Couloir" %} script.sam_cuisine_couloir 
      {% elif states(''input_select.aspi_''+states(''sensor.nameday'')) == "Nettoyage total" %} script.sam_cuisine
      {% endif %} 

Perhaps the double single quotes are your issue?

''input_select.aspi_''

They should just be single single quotes everywhere.

thank you it worked finally.
But i don’t understand why because all my other codes have double quote.
The most important is that it’s working so many thanks to all. :stuck_out_tongue_closed_eyes: :crazy_face: :stuck_out_tongue_winking_eye: :stuck_out_tongue: :hugs:

Double quotes are fine, single quotes as well. Just not 2x single quotes right behind each other.

I’m afraid that the GUI editor does that a LOT !
:man_shrugging:

2 single quotes is called escaping a quote. It turns it into a single quote. It’s valid.