Automation, Variables and Triggers: Message malformed: required key not provided @ data['triggers']

Hi,

I wanted to streamline one of my automations by introducing variables. I have read the documentation, but I might not fully understand it - obviously. :thinking:

I want to create an automation, which manages the roof top window shutters. So, I took my working Automation, changed the logic slightly and tried to introduce variables. Now, I get the error message.

Message malformed: required key not provided @ data[‘triggers’]

Can someone explain, what is wrong with my automation?

alias: Dach Rollo Management v10
description: |-
  Based on sun irridiation W/m2, window shutters are opened/closed in 4 steps.
  no number -> stop 
  0 - 150 -> fully open
  150 - 300 -> 1/3-closed
  300 - 450 -> 50%-closed
  450 - 600 -> 2/3-closed
  600+ -> 100%-closed
mode: single
max_exceeded: silent
variables:
  outside_sun_irradiation: '{{ float( states("sensor.zurich_kloten_sun_irradiation"), "missing value" ) }}'
  outside_temp: '{{ float( states("sensor.zurich_kloten_temperature"), "missing value" ) }}'
  inside_dachzimmer_temp: '{{ float( states("sensor.esphome_dachzimmer2_temperatur"), "missing value" ) }}'
triggers:
  - trigger: state
    entity_id: 
      - sensor.zurich_kloten_sun_irradiation 
  - trigger: state
    entity_id: 
      - weather.winterthur
  - trigger: state
    entity_id: 
      - sensor.esphome_dachzimmer2_temperatur
  - trigger: sun
    event: sunset
    offset: 0
  - trigger: sun
    event: sunrise
    offset: 0
conditions: []
actions:
  - choose:
      - conditions:
        - condition: template
          value_template: >-
            {{ outside_sun_irradiation == 'missing value' or inside_dachzimmer_temp == 'missing value' or outside_temp == 'missing value' }}
          sequence:
            - stop: >-
                Sun irradiation ( {{ outside_sun_irradiation }} ) bzw. Aussentemperatur ( { outside_temp } )bzw. Temperatur Sensor im Dach ( {{ inside_dachzimmer_temp }} ) liefert keine Zahlenwerte.
              error: true
            - delay:
                hours: 0
                minutes: 15
                seconds: 0
                milliseconds: 0
          alias: If sensors do not provide numbers, do nothing
      - conditions:
          - condition: template
            value_template: >-
              {{
              ( as_timestamp(states('sensor.sun_next_setting')) + 3600 - as_timestamp(now()) ) / 3600 < 1
              and ( as_timestamp(states('sensor.sun_next_rising')) + 3600 - as_timestamp(now()) ) / 3600 > 1
              and now().month in [4, 5, 6, 7, 8, 9]
              }}
        sequence:
          - metadata: {}
            data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: If 1h before next sunset and more then 1h before next sun rising and during summer months -> open shutters 
      - conditions:
          - condition: template
            value_template: >-
              {% if states('weather.winterthur') in ['exceptional', 'hail',
              'lightning', 'lightning-rainy', 'pouring', 'rainy', 'snowy',
              'snowy-rainy', 'windy', 'windy-variant', 'unavailable'] %} 
                {{ true }} 
              {% else %}
                {{ false }}
              {% endif %}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 2
              minutes: 0
              seconds: 0
              milliseconds: 0
        alias: If weather conditions are bad, open window shutters
      - conditions:
          - condition: template
            value_template: >-
              {{
              ( as_timestamp(states('sensor.sun_next_dusk')) + 3600 - as_timestamp(now()) ) / 3600 < 22
              and ( as_timestamp(states('sensor.sun_next_rising')) + 3600 - as_timestamp(now()) ) / 3600 > 1
              and outside_temp < 5
              and now().month in [10, 11, 12, 1, 2, 3]
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If Month's in Winter and during night time and outside temp below 5°C, close the shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 600 and inside_dachzimmer_temp > 24 }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 0
              minutes: 30
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is high, fully close window
          shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 450 and inside_dachzimmer_temp > 23.5 }}
        sequence:
          - data: {}
            action: script.dach_rollos_66_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is above medium, close window
          shutters 66%
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 300 and inside_dachzimmer_temp > 23 }}
        sequence:
          - data: {}
            action: script.dach_rollos_50_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is medium, close window
          shutters 50%
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 150 and inside_dachzimmer_temp > 22.5 }}
        sequence:
          - data: {}
            action: script.dach_rollos_33_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is bellow medium, close
          window shutters 33%
      - conditions:
          - condition: template
            value_template: >-
              {{ 150 > outside_sun_irradiation or 22.5 > inside_dachzimmer_temp }}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is low, fully open window
          shutters
    default:
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - stop: no significant change in state OR error in source data type

Hello schneich,

Well for your variables you are casting the sensors to floats, which is good, but your default is text,

which defeats the purpose of a default. Generally you put a number there or don’t have a default. In this form if the number is missing the automation will break.

Thar doesn’t match the reported error though.
Are you sure you reloaded automations after editing this or saved it if you are using the UI?

Hi @Sir_Goodenough ,

Thank you for your input and sorry for being too unspecific.

I have edited the automation in VSCode and then copied it into the YAML-automation-editor. I was not able to save it.

‘missing value’ was my try to do some kind of error handling. When creating template sensors, this has been working as intended.

So, I have re-done my automation and removed the variables and changed the logic again a little bit. This version is now running as intended.

There are two sensors repeating several times, e.g.:

{{ states('sensor.zurich_kloten_sun_irradiation') | float >= 450
and states('sensor.esphome_dachzimmer2_temperatur') | float > 23.5 }}

(first is outside sun irradiation levels and the second sensor is inside temperature)

How would you introduce variables for these two sensors within the following automation?

alias: Dach Rollo Management v10
description: |-
  Based on sun irridiation W/m2, window shutters are opened/closed in 4 steps.
  no number -> stop 
  0 - 150 -> fully open
  150 - 300 -> 1/3-closed
  300 - 450 -> 50%-closed
  450 - 600 -> 2/3-closed
  600+ -> 100%-closed
triggers:
  - entity_id:
      - sensor.zurich_kloten_sun_irradiation
    for:
      hours: 0
      minutes: 0
      seconds: 0
    trigger: state
  - entity_id:
      - weather.winterthur
    trigger: state
  - trigger: state
    entity_id:
      - sensor.esphome_dachzimmer2_temperatur
  - trigger: state
    entity_id:
      - sensor.sun_next_setting
      - sensor.sun_next_rising
conditions: []
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ not ( is_number(states('sensor.zurich_kloten_sun_irradiation'))
              or is_number(states('sensor.esphome_dachzimmer2_temperatur')) or
              is_number(states('sensor.zurich_kloten_temperature')) ) }}
        sequence:
          - stop: >-
              Sun irradiation ( {{
              states('sensor.zurich_kloten_sun_irradiation') }} ) bzw.
              Aussentemperatur ( { states('sensor.zurich_kloten_temperature') }
              )bzw. Temperatur Sensor im Dach ( {{
              states('sensor.esphome_dachzimmer2_temperatur') }} ) liefert keine
              Zahlenwerte.
            error: true
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: If sensors do not provide numbers, stop automation
      - conditions:
          - condition: template
            value_template: >-
              {{ ( as_timestamp(states('sensor.sun_next_setting')) + 3600 -
              as_timestamp(now()) ) / 3600 < 1 and (
              as_timestamp(states('sensor.sun_next_rising')) + 3600 -
              as_timestamp(now()) ) / 3600 > 1 and now().month in [4, 5, 6, 7,
              8, 9] }}
            alias: >-
              If 1h before next sunset and more then 1h before next sun rising
              and during summer months -> open shutters
        sequence:
          - metadata: {}
            data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
      - conditions:
          - condition: template
            value_template: >-
              {% if states('weather.winterthur') in ['exceptional', 'hail',
              'lightning', 'lightning-rainy', 'pouring', 'rainy', 'snowy',
              'snowy-rainy', 'windy', 'windy-variant'] %} 
                {{ true }} 
              {% else %}
                {{ false }}
              {% endif %}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
        alias: If weather conditions are bad, open window shutters
      - conditions:
          - alias: >-
              If Month's in Winter and during night time and outside temp below
              5°C, close the shutters
            condition: template
            value_template: >-
              {{ ( as_timestamp(states('sensor.sun_next_dusk')) + 3600 -
              as_timestamp(now()) ) / 3600 < 22

              and ( as_timestamp(states('sensor.sun_next_rising')) + 3600 -
              as_timestamp(now()) ) / 3600 > 1

              and states('sensor.zurich_kloten_temperature') | float < 5 and
              now().month in [10, 11, 12, 1, 2, 3] }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
        alias: >-
          If month is in winter and during night time and outside temp below
          5°C, close the shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.zurich_kloten_sun_irradiation') | float >= 600
              and states('sensor.esphome_dachzimmer2_temperatur') | float > 24
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is high, fully close window
          shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.zurich_kloten_sun_irradiation') | float >= 450
              and states('sensor.esphome_dachzimmer2_temperatur') | float > 23.5
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_66_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is above medium, close window
          shutters 66%
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.zurich_kloten_sun_irradiation') | float >= 300
              and states('sensor.esphome_dachzimmer2_temperatur') | float > 23
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_50_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is medium, close window
          shutters 50%
      - conditions:
          - condition: template
            value_template: >-
              {{ states('sensor.zurich_kloten_sun_irradiation') | float >= 150
              and states('sensor.esphome_dachzimmer2_temperatur') | float > 22.5
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_33_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is bellow medium, close
          window shutters 33%
      - conditions:
          - condition: template
            value_template: >-
              {{ 150 > states('sensor.zurich_kloten_sun_irradiation') | float
              and 22.5 > states('sensor.esphome_dachzimmer2_temperatur') | float
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is low, fully open window
          shutters
    default:
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - stop: no significant change in state OR error in source data type
mode: single
max_exceeded: silent

hm, I was just able to re-introduce the variables and save the automation. I don’t know, what was wrong before, but now it seems to work fine. First traces look good.

For the record, this is the currently working automation.

alias: Dach Rollo Management v10
description: |-
  Based on sun irridiation W/m2, window shutters are opened/closed in 4 steps.
  no number -> stop 
  0 - 150 -> fully open
  150 - 300 -> 1/3-closed
  300 - 450 -> 50%-closed
  450 - 600 -> 2/3-closed
  600+ -> 100%-closed
triggers:
  - entity_id:
      - sensor.zurich_kloten_sun_irradiation
    for:
      hours: 0
      minutes: 0
      seconds: 0
    trigger: state
  - entity_id:
      - weather.winterthur
    trigger: state
  - trigger: state
    entity_id:
      - sensor.esphome_dachzimmer2_temperatur
conditions: []
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: |-
              {{ not ( 
                      is_number( outside_sun_irradiation )
                      or is_number( inside_dachzimmer_temp ) or
                      is_number( outside_temp )
                    ) }}
        sequence:
          - stop: >-
              Sun irradiation ( {{ outside_sun_irradiation }} ) bzw.
              Aussentemperatur ( {{ outside_temp }} ) bzw. Temperatur Sensor im
              Dach ( {{ inside_dachzimmer_temp }} ) liefert keine Zahlenwerte.
            error: true
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: If sensors do not provide numbers, stop automation
      - conditions:
          - alias: >-
              during summer months, when sun passes a certain angle -> open
              shutters
            condition: template
            value_template: >-
              {{ state_attr('sun.sun','azimuth') | float > 270 and now().month
              in [4, 5, 6, 7, 8, 9] }}
        sequence:
          - metadata: {}
            data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
      - conditions:
          - condition: template
            value_template: >-
              {% if states('weather.winterthur') in ['exceptional', 'hail',
              'lightning', 'lightning-rainy', 'pouring', 'rainy', 'snowy',
              'snowy-rainy', 'windy', 'windy-variant'] %} 
                {{ true }} 
              {% else %}
                {{ false }}
              {% endif %}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 1
              minutes: 0
              seconds: 0
              milliseconds: 0
        alias: If weather conditions are bad, open window shutters
      - conditions:
          - alias: >-
              during winter months, after 20:00 and outside temp below 5°C ->
              close shutters
            condition: template
            value_template: |-
              {{ outside_temp < 5 and
                ( 20 < now().hour or 
                  ( now() < (states('sensor.sun_next_rising') | as_datetime | as_local)
                  and now().day == (states('sensor.sun_next_rising') | as_datetime | as_local).day
                  and state_attr('sun.sun','elevation') | float < -8 )
                )
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          during winter months, after 20:00 and outside temp below 5°C -> close
          shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 600 and inside_dachzimmer_temp > 24
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is high, fully close window
          shutters
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 450 and inside_dachzimmer_temp >
              23.5 }}
        sequence:
          - data: {}
            action: script.dach_rollos_66_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is above medium, close window
          shutters 66%
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 300 and inside_dachzimmer_temp > 23
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_50_zu_v2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is medium, close window
          shutters 50%
      - conditions:
          - condition: template
            value_template: >-
              {{ outside_sun_irradiation >= 150 and inside_dachzimmer_temp >
              22.5 }}
        sequence:
          - data: {}
            action: script.dach_rollos_33_zu_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is bellow medium, close
          window shutters 33%
      - conditions:
          - condition: template
            value_template: >-
              {{ 150 > outside_sun_irradiation and 22.5 > inside_dachzimmer_temp
              }}
        sequence:
          - data: {}
            action: script.dach_rollos_auf_2
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
        alias: >-
          If sun irradiation and indoor temperatur is low, fully open window
          shutters
    default:
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - stop: no significant change in state OR error in source data type
variables:
  outside_sun_irradiation: >-
    {{ float( states('sensor.zurich_kloten_sun_irradiation'), "missing value" )
    }}
  outside_temp: "{{ float( states('sensor.zurich_kloten_temperature'), \"missing value\" ) }}"
  inside_dachzimmer_temp: >-
    {{ float( states('sensor.esphome_dachzimmer2_temperatur'), "missing value" )
    }}
mode: single
max_exceeded: silent

The variables have been moved to the end of the script, when saving in the GUI-editor. HomeAssistent saves them in two different layouts. I had added them like this:

variables:
  outside_sun_irradiation: >-
    {{ float( states('sensor.zurich_kloten_sun_irradiation'), "missing value" ) }}
  outside_temp: >-
    {{ float( states('sensor.zurich_kloten_temperature'), "missing value" ) }}
  inside_dachzimmer_temp: >-
    {{ float( states('sensor.esphome_dachzimmer2_temperatur'), "missing value" ) }}