Automation turns light on again with light.turn_off action

Hi guys,

I have a strange behaviour with my automation. And maybe it’s very simple but at the moment I can’t see it.

My automation shall be triggered by motion off for 2 minutes. So far so good.

Then it should dim the lights. Wait for 2 minutes and then switch off the light.

This seems to work so far. My problem is that immediately after the switch off. The light is switched on again. And I have no clue why.



Any ideas?

Thanks in advance.

spuder

Hi @spud-er,

This looks strange!

I guess you chose the restart mode in order to start over again if the motion sensor is activated again and been off for 2 minutes. But I am not sure whether there could be an issue in the logic if you start over before completing all steps.

So, could you please try first to change the automation mode from restart to single and see whether this has any effect.

And are you sure you don’t have other automations related to the light entity? You can see that on the device page like here

Actually, I will suggest instead of doing the delay, that you have two triggers in the same automation. One for the sensor has been off for 2 min, and the other for being off for 4 minutes.
Then the automations is completed by one action. No delay that holds the thread and might impact the performance and no more need for using the restart mode.

I made the following version for you, please make sure I spelled your entity names correct

alias: Stepwise turn light off
description: ''
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.flur_unten_hue_bewegungssensor_bewegungssensor
    to: 'off'
    for:
      minutes: 2
    id: '2'
  - platform: state
    entity_id: binary_sensor.flur_unten_hue_bewegungssensor_bewegungssensor
    to: 'off'
    for:
      minutes: 4
    id: '4'
condition:
  - condition: state
    entity_id: light.flur_unten_dc
    state: 'on'
action:
  - service: light.turn_on
    data:
      brightness: 50
  - choose:
      - conditions:
          - condition: trigger
            id: '2'
        sequence:
          - service: light.turn_on
            data:
              transition: 5
              brightness: '{{ states(''input_number.input_helligkeit_nacht'') | int }}'
            target:
              entity_id: light.flur_unten_dc
    default:
      - service: light.turn_off
        data:
          transition: 10
        target:
          entity_id: light.flur_unten_dc

I tried it with my entities and it works just fine for me.

Please try it and let me know if it works.

Kind regards,
Ghassan

Hi Ghasan,

first of all I checked whether there is another automation that crossfires my one.

I have another one that regulates the brightness of all lights in the house. Whenever one light is switched on. The brightness is set by a template in this general automation

I switched it off and my automation works as charm. So the problem seems to be the sequence.

I guess as long as my “switch off” automation is running (delay) the general brightness automation is waiting for it until its finished and fires its own brightness level. But I’m not 100% sure. But it would explain the behaviour.

I tried it with your code as well. I deleted your first action. (it didn’t work with it). Afterwards the behaviour seems to be the same. It works when general brightness automation is off. And it doesn’t when it is on.

Nevertheless. Thanks for your help. You put my nose to the right point. And a second thank you for the inspiration of 2 triggers. I wasn’t aware that this is an option in home assistant. I will further investigate on this.

spuder

Ok. I made some adjustments. And I think I got it. I put now everything in one automation.

Multiple triggers in combination with choose are pure gold. Thanks again @ghassan. :pray:

Solution is to switch off the impacting automation and switch it on again when everything is done.

My last hurdle was the transition in light.switch_off. It seems to execute light.turn_on services for every step. And the automation seems not to wait until transition is completed. So I needed a last delay.

Maybe it helps someone else. So I add my code here.

Motion sensor automation with stepwise dimming.

greetz spuder

- alias: "Flur unten Bewegungssteuerung"
  mode: restart
  trigger:
    - platform: state
      entity_id: binary_sensor.flur_unten_hue_bewegungssensor_bewegungssensor
      to: "on"
      id: "0"
    - platform: state
      entity_id: binary_sensor.flur_unten_hue_bewegungssensor_bewegungssensor
      to: "off"
      for: 30
      id: "1"
    - platform: state
      entity_id: binary_sensor.flur_unten_hue_bewegungssensor_bewegungssensor
      to: "off"
      for: 60
      id: "2"
  condition:
    - condition: numeric_state
      entity_id: sensor.flur_unten_hue_bewegungssensor_helligkeit
      below: 15
  action:
    - service: automation.turn_off
      entity_id: automation.haus_lampen_helligkeit
    - choose:
        - conditions:
            - condition: trigger
              id: "0"
          sequence:
            - service: light.turn_on
              entity_id: light.flur_unten_dc
              data:
                transition: 0
                brightness: >
                  {% if is_state('input_select.haus_eco_modus', 'nachts') %}
                    {{ states('input_number.input_helligkeit_nacht') }}
                  {% elif is_state('input_select.haus_eco_modus', 'eco max') %}
                    {{ states('input_number.input_helligkeit_niedrich') }}
                  {% elif is_state('input_select.haus_eco_modus', 'eco') %}
                    {{ states('input_number.input_helligkeit_hoch') }}
                  {% elif is_state('input_select.haus_eco_modus', 'aus') %}
                    255
                  {% else %}
                    255
                  {% endif %}
        - conditions:
            - condition: trigger
              id: "1"
          sequence:
            - service: light.turn_on
              entity_id: light.flur_unten_dc
              data:
                transition: 5
                brightness: "{{ states('input_number.input_helligkeit_nacht') | int }}"
            - delay: 6
        - conditions:
            - condition: trigger
              id: "2"
          sequence:
            - service: light.turn_off
              data:
                transition: 10
              target:
                entity_id: light.flur_unten_dc
            - delay: 11
      default:
        - service: light.turn_off
          data:
            transition: 0
          target:
            entity_id: light.flur_unten_dc
    - service: automation.turn_on
      entity_id: automation.haus_lampen_helligkeit

Hi @spud-er

This following automation works just fine for me, without delays and have no issues with the light automatically turning on again after turning off, eve though i use the transition.

alias: office light control
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.0x158d0006ba9695_motion
    to: 'on'
    id: '1'
  - platform: state
    entity_id: binary_sensor.0x158d0006ba9695_motion
    to: 'off'
    for:
      minutes: 2
    id: '2'
  - platform: state
    entity_id: binary_sensor.0x158d0006ba9695_motion
    to: 'off'
    for:
      minutes: 4
    id: '4'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: '1'
        sequence:
          - service: light.turn_on
            data:
              transition: 5
              brightness_pct: 100
            target:
              entity_id:
                - light.gaestevaerelse
      - conditions:
          - condition: trigger
            id: '2'
        sequence:
          - service: light.turn_on
            data:
              transition: 5
              brightness_pct: 50
            target:
              entity_id:
                - light.gaestevaerelse
    default:
      - service: light.turn_off
        data:
          transition: 10
        target:
          entity_id:
            - light.gaestevaerelse
mode: single

So I guess you make it more complex than necessary, and if using my code does not eliminate the issue with auto turning the light on, after turning it off, there might be a reason that need to be found. What kind of light bulbs/integration is it (Philips Hue, Tuya, other )

Example of unnecessary code complexity is that the default choose condition will never be entered as the trigger can only be 0,1 or 2 which are handled already.

Kind regards,
Ghassan

Hi @ghassan

for this room I use innr bulbs (zigbee) with deCONZ Stick.

Regarding the complexity you are right. This is just a spleen not to use the else condition. I prefer to have all options as elif. The idea is that in case I change code there is some kind of “emergency exit”.

As I wrote my problem is that there is a second automation responsible for brightness of all lights including the lights in the motion automation.

And when transition is like a sequence …

  • light.turn_on (brightness 50)
  • light.turn_on (brightness 25)
    and so on

then the second automation starts working and this behaviour could be the result. I’ll have another look tomorrow.

BR spuder

Hi @spud-er

Arh, I did not realize before that there is another automation on same light entities.

Now I understand your approach.

Well done :+1:

Kind regards,
Ghassan