Home assistant automation is_state_attr

Hi

I try to write automation for button to change color on lamp.

  • id: ‘1531570360460’
    alias: test
    trigger:
    • event_data:
      event: 4002
      id: vrumtakremote
      event_type: deconz_event
      platform: event
      condition: []
      action:
    • data:
      entity_id: light.vrumtak
      {% if is_state_attr(‘light.vrumtak’, ‘color_temp’, ‘250’) %}color_temp: 454{% endif %}
      service: light.turn_on

But hass complain on my auto script on is_state_attr row…
What i doing wrong???

//

First, you can’t conditionally include a parameter in a service call. Second, you really need an else part to your if statement. The following will change the color temp to 454 if it is currently 250, or will leave it the same if it’s not 250.

- id: '1531570360460'
  alias: test
  trigger:
    event_data:
      event: 4002
      id: vrumtakremote
    event_type: deconz_event
    platform: event
  condition: []
  action:
    data:
      entity_id: light.vrumtak
      color_temp:
        {% if states.light.vrumtak.attributes.color_temp|int == 250 %}
          454
        {% else %}
          {{ states.light.vrumtak.attributes.color_temp }}
        {% endif %}
    service: light.turn_on

EDIT: Actually, now that I think about it, if the light happens to be off when this automation runs, it won’t work, because a light that is off won’t have a color_temp attribute. So maybe the following would work better:

- id: '1531570360460'
  alias: test
  trigger:
    event_data:
      event: 4002
      id: vrumtakremote
    event_type: deconz_event
    platform: event
  condition: []
  action:
    data:
      entity_id: light.vrumtak
      color_temp:
        {% set ct = state_attr('light.vrumtak', 'color_temp')|int %}
        # If light was on and color temp was 250,
        # change to 454
        {% if ct == 250 %}
          454
        # If light was off ct will evaluate as zero,
        # so use some default value
        {% elif ct == 0 %}
          300
        # If light was on, but color temp was not 250,
        # then leave as is
        {% else %}
          {{ ct }}
        {% endif %}
    service: light.turn_on

This uses the state_attr() function, which will return None if the attribute doesn’t exist. None cast to an integer will evaluate to the value zero.

Ahh.
Now I understand.
Thanks. is look good…

1 Like