First script with data_template

Hello!

I’m trying to figure out how to use templates inside a script. So far it’s nothing fancy but when I save this script, I get this error:

Message malformed: expected dict for dictionary value @ data['sequence'][0]['data_template']

Here is the script. What is wrong? Thank you!

alias: CUISINE set state
sequence:
  - service: light.turn_on
    entity_id: light.bulbes_cuisine_zha_group_0x0005
    data_template: >-
      {% if states.light.bulbes_cuisine_zha_group_0x0005.attributes.brightness > '128') %}
        brightness: 254
      {% else %}
        brightness: 129
      {% endif %}
mode: single

data_template: was deprecated many releases ago in favor of data:. In addition, that’s not the right way to use templates in a data: block, and there are a few other issues. Try this:

alias: CUISINE set state
sequence:
  - service: light.turn_on
    entity_id: light.bulbes_cuisine_zha_group_0x0005
    data:
      brightness: >-
        {% if state_attr("light.bulbes_cuisine_zha_group_0x0005", "brightness")|float > 128 %}
          254
        {% else %}
          129
        {% endif %}
mode: single

The key points are:

  • Don’t quote a number if you’re trying to compare the magnitude. You’re basically turning it into a string.
  • Attributes can retain native types, but it’s usually best to convert it to the type you want with a filter (like |float)
  • Using states(“xxxx.xxx”) and state_attr(“xxx.xx”, “yyy”) is better than using dot notation because it handles unknown/unavailable values better (see warning here)
  • The data: block allows you to use templates for items within that block.
  • Templates return a value (can be a native type, like an integer, string, list, etc.), but not a mapping like “xxx: yyy” as you had.
1 Like