Multiline templates in variables: block

Hi,

I am trying to use the variables: statement in an automation wo define some variables to be used in actions.

This template block is meant to make sure that if I have an eco_off time of e.g. 7 am and eco_on after midnight, then I can still compare now() with both the input_datetime times. Maybe there is a much better way of doing it…

For some reason my script gets the error:

Logger: homeassistant.config
Source: config.py:424
First occurred: 22:52:48 (22 occurrences)
Last logged: 23:05:47

Invalid config for [automation]: Expected a dictionary @ data['condition'][1]['conditions'][0]. Got None expected a dictionary for dictionary value @ data['variables']. Got None. (See /config/configuration.yaml, line 8).
Invalid config for [automation]: expected a dictionary for dictionary value @ data['variables']. Got None. (See /config/configuration.yaml, line 8).

I think it’s coming from the multiline template defining the variable on_ts. If it’s indentation, then I have no idea how indentation in multiline templates is supposed to work. Maybe it’s a question of scope? Can I use variable defined in the same variables: block if they are defined on a line before?

I’ve tried redefining off_ts inside the template with temp_ts, and even naming it something else, but this didn’t help.

Any help appreciated:

- id: auto_heating_eco_off_back
  alias: "Heating: ECO off when someone comes back" 
  description: Leave ECO mode heating if someone comes back
  trigger:
    - platform: state
      entity_id: group.inhabitants
      to: 'on' 
  # Define times in terms of integers of timestamps. Take into account adjustemnt of on time is after midnight
  variables:
    now_ts: "{{ as_timestamp(now()) | int }}"
    off_ts: "{{as_timestamp( now().strftime('%Y-%m-%d ') + states('input_datetime.heating_eco_off')[0:5] ) | int}}"
    on_ts: >
            {% set temp_ts = as_timestamp( now().strftime('%Y-%m-%d') +' '+ states('input_datetime.heating_eco_on')[0:5] ) | int %}
            {% if temp_ts<= off_ts %} {% set temp_ts = temp_ts + 86400 %} {% endif %}
            {{temp_ts}}
    off_holiday_ts: "{{as_timestamp( now().strftime('%Y-%m-%d') +' '+ states('input_datetime.heating_eco_off_holiday')[0:5] ) | int}}"
    on_holiday_tomorrow_ts: >
            {% set temp_ts = as_timestamp( now().strftime('%Y-%m-%d') +' '+ states('input_datetime.heating_eco_on_holiday_tomorrow')[0:5] ) | int %}
            {% if temp_ts<= off_ts %} {% set temp_ts = temp_ts + 86400 %} {% endif %}
            {{temp_ts}}

  condition: 
    - condition: state
      entity_id: input_boolean.heating_season
      state: "on"
    - condition: or
      conditions:
      - "{{ is_state('binary_sensor.workday', 'on') and (now_ts >= off_ts }}"
      - "{{ is_state('binary_sensor.workday', 'off') and (now_ts >= off_holiday_ts) }}"
    - condition: or
      conditions:
      - "{{ is_state('binary_sensor.workday_tomorrow', 'on')  and (now_ts < on_ts) }}"
      - "{{ is_state('binary_sensor.workday_tomorrow', 'off') and (now_ts < on_holiday_tomorrow_ts)}}"
  action:
    - service: climate.set_preset_mode
      target: 
        entity_id: 
          - climate.trv_salon
          - climate.trv_master
          - climate.trv_bath
      data:
        preset_mode: none
    - service: notify.persistent_notification
      data: 
        title: "ECO off: someone back"
        message: "Indeed"  
  mode: restart

change your conditions to use template conditions, single line template conditions don’t work everywhere.

    - condition: or
      conditions:
      - condition: template
        value_template: "..."

All it was of course was a missing parenthesis inside a condition template. But putting it explicitly as a template condition, as you suggested at least gave an explicit error code and allowed me to find it quickly. So that’s a good learning! Thanks a lot.

Incidentally, is there some syntax highlighting editor that would properly highlight the content of templates. I am using VS code with the Home Assistant extension locally with the config files shared over SMB, but I am not sure it is highlighting anything beyond the YAML structure (which is helpful enough of course).

But the templates are always just treated as strings and therefore stuff like this happens to me every time I edit anything longer.