Automation - door sensor and lights

I have a door sensor upstairs and want to know if the door is open by enabling my philips hue light and color it red. The automation I created turns on the red light, but it doesn’t swich back to the original state. which can be either off or at another color.

Is it possible to turn back to the original state of the light when the door is closed?

This is my script:


alias: Light on when door is open
description: enable red light
trigger:
type: opened
platform: device
device_id: bf86fa05a7b6b850d03e3f730e3e90f1
entity_id: binary_sensor.deurcontact_tussendeur_contact
domain: binary_sensor
condition: []
action:
scene: scene.woonkamer_robijngloed
mode: single


Yes.

  • When the door is opened, the automation creates a snapshot scene to record the light’s current state then it turns on scene.woonkamer_robijngloed.

  • When the door is closed, the automation turns on the snapshot scene (which restores the light’s original state).

The last example in “Creating scenes on the fly” shows how to create a snapshot scene. In your case, it will look like this:

  - service: scene.create
    data:
      scene_id: before
      snapshot_entities:
        - light.your_light

Let me know if you need more help to compose the automation.

Thank you for the information.
I am afraid I need a bit more help.
Do I need to edit the scene that I use now to color the light ?
Then add this code to it ?
How to I make sure it activates the snapshot when closing the door (sensor)?

Here’s an example of an automation that triggers whenever the door opens or closes.

alias: Light controlled by door
description: Turn on/off red light
trigger:
  - platform: state
    entity_id: binary_sensor.deurcontact_tussendeur_contact
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.deurcontact_tussendeur_contact
    from: 'on'
    to: 'off'
condition: []
action:
  - choose: 
      - conditions: "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - service: scene.create
            data:
              scene_id: before
              snapshot_entities:
                - light.your_light
          - service: scene.turn_on
            target:
              entity_id: scene.woonkamer_robijngloed
    default:
      - service: scene.turn_on
        target:
          entity_id: scene.before
mode: single

How it works

  • When the door opens, it saves the current state of light.your_light (replace this with whatever is the name of the light in your scene called scene.woonkamer_robijngloed) to a new scene named scene.before. Then it turns on scene.woonkamer_robijngloed.

  • When the door closes, it turns on scene.before which restores light.your_light to its original state.

Thanks for your fast reaction.
When replacing the Yaml script with the one you created, I get the following error when saving the automation. I tried to fix it, but so far the error keeps appearing.

Message malformed: required key not provided @ data[‘action’]

Sorry about that; I made a mistake in the choose.

I have corrected the error in the original example above; please try the corrected version.

Thank you for the adjustment.
The error is now gone, but the light doesn’t revert back to the previous stage.
If you have any idea, I’d love to know.

What is entity name of the light?

Hi,
Thanks for your help so far.
I have rebooted my Pi yesterdayevening and now zigbee2mqtt doesn’t seem to function right. I get an error and need to fix that first, as I don’t get all information back from the sensors. I’ll give it a try once everyting is working fine again.

(node:7) UnhandledPromiseRejectionWarning: Error: SREQ '--> AF - dataRequestExt - {"dstaddrmode":2,"dstaddr":"0x000000000000fffd","destendpoint":242,"dstpanid":0,"srcendpoint":242,"clusterid":33,"transid":10,"options":0,"radius":30,"len":6,"data":{"type":"Buffer","data":[25,11,2,11,254,0]}}' failed with status '(0x02: INVALID_PARAM)' (expected '(0x00: SUCCESS)')
    at Object.func (/app/node_modules/zigbee-herdsman/src/adapter/z-stack/znp/znp.ts:322:27)
    at Queue.executeNext (/app/node_modules/zigbee-herdsman/src/utils/queue.ts:32:32)
(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 10)