New error after upgrade - not a valid value for dictionary value @ data['entity_id']

I’ve recently upgraded to Home Assistant 2022.2.5 and now some of my automations aren’t working for my thermostats. I’m getting an error in the logs “not a valid value for dictionary value @ data[‘entity_id’]” - has something changed with how data templates are used? I have not adjusted entities or the values in automations.yaml. I did just comment out a device (Christmas lights) which is not plugged in right now, but it’s still not working. It says the error is in pos 6 - not sure which row to start counting from and which rows to include/exclude to determine the source of the error but they’re mostly similar:

- id: family_came_home
  alias: Set thermostat when family changes to home
  trigger:
  - platform: state
    entity_id: input_select.family
    to: Home
  action:
  - service: scene.turn_on
    data_template:
      entity_id: "{%if states.sensor.openweathermap_temperature.state|float > 78 %}\n\
        \  scene.athomehot_all\n{% elif states.sensor.openweathermap_temperature.state|float\
        \ < 66 %}\n  scene.athomecool_all\n{% else %}\n  scene.athome_all\n{% endif\
        \ %}"
  - delay: 00:00:15
  - service: scene.turn_on
    data_template:
      entity_id: "{%if states.sensor.openweathermap_temperature.state|float > 78 %}\n\
        \  scene.athomehot_all\n{% elif states.sensor.openweathermap_temperature.state|float\
        \ < 66 %}\n  scene.athomecool_all\n{% else %}\n  scene.athome_all\n{% endif\
        \ %}"
  - delay: 00:00:15
  - service: scene.turn_on
    data_template:
      entity_id: "{%if states.sensor.openweathermap_temperature.state|float > 78 %}\n\
        \  scene.athomehot_all\n{% elif states.sensor.openweathermap_temperature.state|float\
        \ < 66 %}\n  scene.athomecool_all\n{% else %}\n  scene.athome_all\n{% endif\
        \ %}"
  - service: scene.turn_on
    data_template:
      entity_id: '{%if is_state(''sun.sun'', ''below_horizon'') %} scene.patio_lights
        {% endif %}'

Wow that was hard to read. I think your main problem was that your last action is missing an else case. If the sun is above the horizon you are calling a scene with no entity id specified.

I wrote your automation out in a more readable form below. Other things to note:

You have not heeded this warning:

https://www.home-assistant.io/docs/configuration/templating/#states

Also you dont need to use data_template any more. Just data will do, though in your case the appropriate key is target.

You have not supplied default values for your filters. I have selected what I deemed appropriate.

- id: family_came_home
  alias: Set thermostat when family changes to home
  trigger:
  - platform: state
    entity_id: input_select.family
    to: Home
  action:
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - delay: 00:00:15
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - delay: 00:00:15
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - service: scene.turn_on
    target:
      entity_id: >
        {% if is_state('sun.sun', 'below_horizon') %}
          scene.patio_lights
        {% else %}
          ####### YOU NEED SOMETHING HERE #######
        {% endif %}

If you don’t have an else case for that last action, do this instead:

- id: family_came_home
  alias: Set thermostat when family changes to home
  trigger:
  - platform: state
    entity_id: input_select.family
    to: Home
  action:
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - delay: 00:00:15
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - delay: 00:00:15
  - service: scene.turn_on
    target:
      entity_id: >
        {% if states('sensor.openweathermap_temperature')|float(0) > 78 %}
          scene.athomehot_all
        {% elif states('sensor.openweathermap_temperature')|float(67) < 66 %}
          scene.athomecool_all
        {% else %}
          scene.athome_all
        {% endif %}
  - condition: state
    entity_id: sun.sun
    state: "below_horizon"
  - service: scene.turn_on
    target:
      entity_id: scene.patio_lights

Awesome, thanks! I didn’t know you could include a condition within the action section. Does that apply to all subsequent actions, so if I also wanted to turn on a switch I could include a switch.turn_on action after that and have that trigger only if the sun is below the horizon?

Yes, if the condition evaluates to false then all subsequent actions will be cancelled.