What's the right way to trigger on an attribute changing to true or false?

Hi team,

I’ve moved towards a “tuple” approach for light automations, where I pair a template binary sensor with a light entity with a timer.

The idea is that when the template sensor turns “on” the light turns on, and when the template sensor turns “off” the automation starts/restarts a timer. When the timer expires, the light turns off.

The idea is have a much more high fidelity and involved automation rather than just seeing motion, because you can do arbitrary boolean logic.

I’ll include an example of one of the sensor at the bottom of this post. If anyone wants the blueprint, let me know.

The binary sensor has a few attributes on it in addition to its state. One of those attributes is an “eligible” attribute. The idea here is that the light only turns on when the state changes to “on” if “eligible” evaluates to true. Also, if eligible ever changes to false, the light immediately turns off rather than starting the timer.

The idea behind that is let’s say you want your lights off when you start sleeping, if you have an input_boolean called “sleeping”, you want those lights to turn off immediately rather than after the timer expires, unlike during the day when you want some duration there.

Anyways, I’m not exactly sure the right way to trigger based off of the attribute. Right now I’m triggering off of any state changes at all on it, but I’d like to do something similar to the state where I can say “to: ‘on’”. I know that state in binary sensors is treated special, with “on” and “off” but I didn’t think that extended to attributes.

Sample template sensor:

    - name: kitchen_sink_light_settings
      state: >
        {% set motion = is_state('binary_sensor.kitchen_motion_occupancy_group', 'on') %}
        {% set dark_outside = is_state('binary_sensor.is_dark_outside', 'on') %}
        {% set shade_closed = is_state('cover.kitchen_covers', 'closed') %}
        {{ motion and (dark_outside or shade_closed) }}
        color_name: "white"
        normal_brightness: 60
        low_brightness: 30
        transition: 3
        eligible: >
          {% set someone_home = is_state('binary_sensor.someone_is_home', 'on') %}
          {% set auto = is_state('input_select.home_ambiance', 'Automatic') %}
          {% set sleeping = is_state('input_boolean.sleeping', 'on') %}
          {% set movie = is_state('input_boolean.watching_movie', 'on') %}
          {{ someone_home and auto and not sleeping and not movie }}

My blueprint triggers as they stand now:

  - platform: state
    entity_id: !input trigger_sensor
    to: "on"
    id: "turn-on"
  - platform: state
    entity_id: !input trigger_sensor
    attribute: color_name
    id: "color-changed"
  - platform: state
    entity_id: !input trigger_sensor
    to: "off"
    id: "pending-off"
  - platform: state
    entity_id: !input trigger_sensor
    attribute: eligible
    id: "eligible"
  - platform: event
    id: "timer-finished"
    event_type: timer.finished
      entity_id: !input light_timer
  - platform: state
    entity_id: binary_sensor.lights_should_be_low
    id: "brightness"

Then I have to do for example in the “action”:

  eligible_var: "{{ state_attr(trigger_sensor_var, 'eligible') }}"

I’m hoping to have an explicit trigger for when eligible rotates from false to true and true to false.

Would I use “to: ‘on’” for that (I suspect not) or is there any better way to do it?

Thank you so much in advance everyone.


Unlike states, attributes can be data types other than strings. In the case of your eligible attribute that would be a boolean with valid values true and false. That means you need to be careful when setting up the trigger (especially in the UI automation editor) to make sure it uses the boolean true not the string "true" for your to and from configuration keys.

  - platform: state
    entity_id: !input trigger_sensor
    attribute: eligible
    id: "eligible"
    to: true
    from: false
1 Like

Got it! I didn’t know I could do that in the trigger. That’s great, thank you Drew I appreciate your quick reply. I shall try it out today.