Toggle bulb-color red/green when door changes from locked/unlocked?

I have an aqara contact-sensor in my door-lock and a milight rgb-bulb that I would like to react to the door status. But I can’t figure out how to change the bulb to green when unlocked and red when the door is locked.

binary_sensor.0x00158d0004215383_contact - Door sensor
light.kok_fonsterlampa_milight - lightbulb

Assuming the binary sensor is on when the door is locked:

trigger:
  platform: state
  entity_id: binary_sensor.0x00158d0004215383_contact
action:
  service: light.turn_on
  entity_id: light.kok_fonsterlampa_milight
  data_template:
    color_name: "{{ 'red' if trigger.to_state == 'on' else 'green' }}"

Theoretically you can use data: instead of data_template: if you are running 0.115 but I have seen reports of this not working as intended.

Thanks, but when I try I get: "Message malformed: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘=’) for dictionary value @ data[‘action’][0][‘data_template’]
"

Sorry it should be == not =

color_name: "{{ 'red' if trigger.to_state == 'on' else 'green' }}"

x == y compares the values of x and y
x = y sets x to the value y.

I edited my post in case anyone else tries it without reading this.

Thanks, but I can’t get it to work. The binary sensor is a Aqara door/window sensor configured as a lock. The automation gets triggered every time I lock/unlock but the lightbulb does not change color. I am unsure if it is the the state of the lock that is wrong (I also tried “locked” instead of “on” ). How can I check the actual states that a binary sensors have ?
Can I somehow test to send the color-change command directly to the bulb to check if that is the problem ?

Binary sensor states are always on or off. Though they can be displayed differently in Lovelace if you apply a device class.

You can check the state of your sensors in the Developer Tools / States menu.

Try this, pretty sure it will work now:

color_name: "{{ 'red' if trigger.to_state.state == 'on' else 'green' }}"
1 Like

Ah, thanks now it is working perfectly.

1 Like

Yeah sorry, my mistake.

I know this is a little off topic, but I have noticed something strange. Normally when I lock the door I trigger the Lock-entity called “Main Door” to “Locked/Unlocked” and that one always react correctly, the “Main Door” status triggers the automation " [Change Bulb Color on MaindoorStatus] -> has been triggered by state of binary_sensor.0x00158d0004215383_contact"

But after a while the automation triggers directly from the binary sensor, not the lock entity.
“[Change Bulb Color on MaindoorStatus] -> has been triggered by state of binary_sensor.0x00158d0004215383_contact” without the lock entity changing. The lock entity always shows the correct status.

Can anyone understand why this happens ?

Does your binary sensor go to sleep (e.g. a zigbee or zwave battery powered sensor)?

If so its state becomes ‘unavailable’ and triggers the automation.

Easy to exclude with a condition in the automation if this is the case.

Yes it is a Aqara battery powered zigbee sensor. But in all the false-triggers I have noticed it always turns to red after a while which is “on” if I understand correctly. Also I cant see that the lock gets any random triggers when looking at the history.

Yeah I would have expected green (the catch-all else option).

Wait for your sensor to sleep and look in the dev tools states menu and check its state.

Here’s a condition you could add to you automation to only allow the states on and off:

condition: template
value_template: "{{ trigger.to_state.state in [ 'on', 'off' ] }}"