Automation help please, format error (Solved)

I’m having trouble creating a automation and could use a hand with the format of this automation. Been at it for hours and my head hurts.

This is what i have right now i get an error on the line 15 " {%- else -%} , “found character ‘%’ that cannot start any token”:

###Plant light timer
- id: plant_light_cycle
  alias: Plant lights time cycle
  trigger:
   - platform: template
     value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    data_template : 
      entity_id: >
        {%- if is_state("input_select.plant_light_cycle_choice", "Grow") -%}
      action:
        service: switch.turn_on
        entity_id: switch.plant_lights
        delay: "20:00:00"
      {%- else -%}
        service: switch.turn_on
        entity_id: switch.plant_lights
        delay: "12:00:00"
      {%- endif %}

Many thanks.

{%- endif %}

Maybe put a - there too

{%- endif -%}

I tried.
No joy same error.

Thank you however. :smiley:

Current try:

###Plant light timer
- id: plant_light_cycle
  alias: Plant lights time cycle
  trigger:
   - platform: template
     value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    data_template : 
      entity_id: >
        {%- if is_state("input_select.plant_light_cycle_choice", "Grow") -%}
      action:
        service: switch.turn_on
        entity_id: switch.plant_lights
        delay: "20:00:00"
        service: switch.turn_off
        entity_id: switch.plant_lights
      {%- else -%}
        service: switch.turn_on
        entity_id: switch.plant_lights
        delay: "12:00:00"
        service: switch.turn_off
        entity_id: switch.plant_lights
      {%- endif -%}

You also have action: twice

Try :

###Plant light timer
- id: plant_light_cycle
  alias: Plant lights time cycle
  trigger:
   - platform: template
     value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    service: switch.turn_on
    data_template : 
        {%- if is_state("input_select.plant_light_cycle_choice", "Grow") -%}
        entity_id: switch.plant_lights
        delay: "20:00:00"
      {%- else -%}
        entity_id: switch.plant_lights
        delay: "12:00:00"
      {%- endif %}

You can’t do that.
You need to template each key:

1 Like

Jinja always returns a string, this means you can’t template multiline key/value pairs. As @ludeeus said, each key needs to be templated separately.

Something Like this?:

###Plant light timer
- id: plant_light_cycle
  alias: Plant lights time cycle
  trigger:
   - platform: template
     value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    service: switch.turn_on
    data_template : 
      {%- if is_state("input_select.plant_light_cycle_choice", "Grow") -%}
          entity_id: switch.plant_lights
          delay: "20:00:00"
    data_template : 
      {%- if is_state("input_select.plant_light_cycle_choice", "Flower") -%}
          entity_id: switch.plant_lights
          delay: "12:00:00"		

Many thanks.

No. Like this:

  action:
    service: switch.turn_on
    data_template : 
      entity_id: switch.plant_lights
      delay: >
        {% if is_state("input_select.plant_light_cycle_choice", "Grow") %}
           20:00:00
        {% if is_state("input_select.plant_light_cycle_choice", "Flower") %}
           12:00:00
        {% else %}
           You need something here if there is another choice or if it is undefined.
        {% endif %}

If there are only two choices in your input select pick a default in case it becomes undefined (e.g. “grow” in this example):

  action:
    service: switch.turn_on
    data_template : 
      entity_id: switch.plant_lights
      delay: >
        {% if is_state("input_select.plant_light_cycle_choice", "Flower") %}
           12:00:00
        {% else %}
           20:00:00
        {% endif %}

That works.
Many thanks for all the help everyone.

Hmm.
The format is correct.
However I’m receiving this error on trigger:

Error while executing automation automation.plant_lights_time_cycle. Invalid data for call_service at pos 1: extra keys not allowed @ data['delay']

If it helps.
What I’m trying to achieve is a automation that turns on the plant light ether for 20 hours or 12 hours, at a specified time.

Current code. I changed the target switch and time for testing (the heater is next to me)

- id: plant_light_cycle
  alias: Plant lights time cycle
  trigger:
   - platform: template
     value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    service: switch.turn_on
    data_template : 
      entity_id: switch.portable_heater
      delay: >
        {% if is_state("input_select.plant_light_cycle_choice", "Flower") %}
           00:02:00
        {% else %}
           00:00:30           
        {% endif %}	

Many thanks.

Delay is not part of the “switch.turn_on” service call. Can you even template a delay between service calls in the action part?

1 Like

Yeah you’re right about the service. That was stupid of me. I’m not actually sure you can template a delay.

Edit: yeah you should be able to.

  action:
    - service: switch.turn_on
      entity_id: switch.portable_heater
    - delay: "{% if is_state('input_select.plant_light_cycle_choice', 'Flower') %} 20:00:00 {% else %} 12:00:00 {% endif %}"
    - service: switch.turn_off
      entity_id: switch.portable_heater

Be very careful that the automation does not trigger again while waiting in the delay. Or the delay will be skipped and the switch will turn off. Also restarting home assistant will stop the delay and the lights won’t turn off.

A better way would be to turn the switch on with one automation and use another to turn it off. Like this:

###Plant light timer on
- id: plant_light_cycle_on
  alias: Plant lights time cycle on
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (states.input_datetime.plant_light_start_time.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"
  action:
    service: switch.turn_on
    entity_id: switch.plant_lights

###Plant light timer off
- id: plant_light_cycle_off
  alias: Plant lights time cycle off
  trigger:
    platform: state
    entity_id: switch.plant_lights
    to: 'on'
    for:  "{% if is_state('input_select.plant_light_cycle_choice', 'Flower') %} 20:00:00 {% else %} 12:00:00 {% endif %}"
  action:
    service: switch.turn_off
    entity_id: switch.plant_lights

The thing to note here is that restarting home assistant will reset the off timer to 12 or 20 hours. Have a read of this if that worries you:

2 Likes

Brilliant. works a like a charm!

I will need to read a bit more to fully comprehend the “resume timers after reboot” part.

However,. I’m one step further and my bed is calling me.
Tomorrow I’ll have a good read to digest.

Many thanks.