Presence detection + Saving State Automation

Hi,

I have a presence sensor in my office (Combination PIR + mmWave), single entity.

I’d like my office lights to return to their last known state when I enter the room. Upon leaving, they should re-save their state.

I have different scenes for my Office for when I’m Gaming, versus getting work done… so the state of my lights varies when I leave the Office.

Here’s what I have so far…

alias: Office Occupancy
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.office_occupancy
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.office_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 0
        sequence:
          - service: scene.create
            data:
              scene_id: automated_office_state_lights
              snapshot_entities: >-
                light.office_main_lights light.office_floor_lamp
                light.office_table_lamp
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.office_main_lights
                - light.office_floor_lamp
                - light.office_table_lamp
      - conditions:
          - condition: state
            entity_id: binary_sensor.office_occupancy
            state: "on"
        sequence:
          - service: scene.turn_on
            data: {}
            target:
              entity_id: >-
                {% if states.scene.automated_office_state_lights.state is defined %}
                  scene.automated_office_state_lights
                {% else %}
                  scene.office_working
                {% endif %}
    default: []
mode: single

Have I got this right? Is there a better approach I’m missing?

Thanks!

What is the purpose of default given that the two choices in choose already handle when the binary_sensor is on and off?

Well, my first thought was that for its initial run - the automated scene may not exist, so it should dfault to turning all of the lights on, or use a scene I often use.

But - now thinking about it, perhaps that condition should be in the ‘on’ condition?

I’ve updated the initial post to reflect that.

alias: Office Occupancy
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.office_occupancy
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  - platform: state
    entity_id: binary_sensor.office_occupancy
    from: 'off'
    to: 'on'
condition: []
action:
  - if: "{{ trigger.to_state.state == 'off' }}"
    then:
      - service: scene.create
        data:
          scene_id: automated_office_state_lights
          snapshot_entities:
            - light.office_main_lights
            - light.office_floor_lamp
            - light.office_table_lamp
      - service: light.turn_off
        target:
          entity_id:
            - light.office_main_lights
            - light.office_floor_lamp
            - light.office_table_lamp
    else:
      - service: scene.turn_on
        target:
          entity_id: >
            scene.{{ 'automated_office_state_lights' if states.scene.automated_office_state_lights.state is defined else 'office_working' }}
mode: single

EDIT

Correction. Fixes list of snapshot_entities

You’re a legend! Thank you. Took me a bit to figure out I’d mis-spelled an entity… but it otherwise seems good now!

Thanks again!

1 Like