Is this automation syntax wrong? VS Code says so!

Tags: #<Tag:0x00007f7394dd0fb8> #<Tag:0x00007f7394dd0ec8>

The latest version of VS Code is telling me that this automation condition is wrong. However, it has been working for months. It could be a situation where it is technically wrong but HA is forgiving enough to allow it to work.

The automation should fire if the lights are on and the brightness is below 127. Is there a more correct way to write it?

      # Cabinet lights are on and below 127
      - condition: state
        entity_id: light.kitchen_cabinet_lights
        state: 'on'
      - condition: numeric_state
        entity_id: light.kitchen_cabinet_lights
        value_template: '{% if states.light.kitchen_cabinet_lights.state == "on" %}{{ states.light.kitchen_cabinet_lights.attributes.brightness }}{% else %}127{% endif %}'
        below: 127

It’s complaining that light.kitchen_cabinet_lights doesn’t match a pattern that begins with the word sensor. Maybe there’s more to that regex pattern (beyond the right edge of the screen) that isn’t shown in the screenshot, but there are definitely more domains than just sensor (that can be used with a Numeric State Trigger). Perhaps it’s a bug in the latest version of the Home Assistant Extension for VS Code.

Thanks @123. I found another example of a brightness condition that VS Code seems to be happier with. I have not tested it yet, but it would be something like this:

      - condition: template
        value_template: '{{ states.light.kitchen_cabinet_lights.attributes.brightness | float < 127 }}'

Already reported and fixed by Frenck, but not yet released: https://github.com/keesschollaart81/vscode-home-assistant/issues/441

1 Like

Thanks @rccoleman!

I don’t know how you’re using that condition but if the trigger accepts any state-change, including on to off, that template will experience a problem. When off, the light will have no brightness attribute.

A safer way is to use the state_attr function. It returns None if the attribute doesn’t exist (or even if the entity doesn’t exist).

      - condition: template
        value_template: >
          {% set b = state_attr('light.kitchen_cabinet_lights', 'brightness') %}
          {{ b != none and b | int < 127 }}