Need help: expected dict for dictionary value @ data['action'][0]['data']. Got None

I think I have tomatoes on my eyes :slight_smile: Why do I always get an error

2021-10-04 12:14:23 ERROR (MainThread) [homeassistant.components.automation] Blueprint Heizkoerpersteuerung generated invalid automation with inputs OrderedDict([('sensor_entity', 'binary_sensor.aqara_diele_haustur_contact'), ('climate_target', 'climate.diele_thermostat'), ('minimum_open_time', 1)]): expected dict for dictionary value @ data['action'][0]['data']. Got None

when using this automation/blueprint:

blueprint:
  name: Heizkoerpersteuerung
  description: An automation that toogles a climate device if a window or door sensor is opened or closed.
  domain: automation
  input:
    sensor_entity:
      name: Window or Door Sensor
      description: The window or door sensor that controls the climate entity.
      selector:
        entity:
          domain: binary_sensor
    minimum_open_time:
      name: Miniumum time the window or door is open or close
      description: Time in seconds to wait until the automation is triggered
      default: 30
      selector:
        number:
          min: 0.0
          max: 120.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
    climate_target:
      name: Climate Device
      description: The climate entity that is controlled by the window or door sensor.
      selector:
        entity:
          domain: climate

trigger:
  - platform: state
    entity_id: !input 'sensor_entity'
    for: !input 'minimum_open_time'

condition: 
  - condition: state
    entity_id: input_boolean.heizperiode
    state: 'on'

action:
  - service: climate.set_temperature
    data: >
      {% if trigger.to_state.state == "on" %}
        hvac_mode: heat
        temperature: 12
      {% else %}
        hvac_mode: auto
      {% endif %}
    entity_id: !input climate_target

You can’t template fields, templates can only be applied inside a single field. You should be using choose instead of a template

action:
- choose:
  - conditions:
    - condition: template
      value_template: "{{ trigger.to_state.state =='on' }}"
    sequence:
    - service: climate.set_temperature
      target:
        entity_id: !input climate_target
      data:
        hvac_mode: heat
        temperature: 12
  default:
  - service: climate.set_temperature
    target:
      entity_id: !input climate_target
    data:
      hvac_mode: auto
1 Like

…which me wonder if it would be desirable to be able to have something like this (not a working example)

  - service: climate.set_temperature
    data: >
      {% if trigger.to_state.state == "on" %}
        {"hvac_mode": "heat", "temperature": 12 }
      {% else %}
        {"hvac_mode": "auto" }
      {% endif %}
    

As we have typed templates, the issue is more on validation side than actual functionality.

It would be, however it moves the template resolution to the data node instead of each node below. I’m not sure if a PR like that would get through. But now that typing exists, it should be doable.

Indeed, but the “data” resolution is all in the service itself (https://github.com/home-assistant/core/blob/bf9f55c376ad33a053b542d4f7bad64a6959931f/homeassistant/helpers/service.py#L230), so little code to add, really, I think.

Not really, you’re forgetting config validation. The config is already validated at that point btw.

Sure, I meant actual functionality-wise.

I created a FR to get feedback: Service data: Allow templates returning a dict in addition to a dict of templates

Actually, you only need to change validation :wink:

1 Like