[solved] 2 different automations, 1 light, in some cases wrong result

I have 2 automations that control a light.
n°1 calls a script to dim when pc is offline (away from desk) and switches off some other stuff
n°2 switches the light off and stops the music when I leave the room

It happens when I leave the room after switch pc off (automation n°1 is executed) and shorly after I leave the room I start automation n°2, the light switches back on.
This is because I have a binary sensor for n°1 (ping) with a delay_off of 5 minutes, so this behavior is normal.

Is it possible to not have the light come back on if these 2 automations are started withing this 5 minute frame?

Please share the automation yaml

Hi @sheminasalam

n°1

- id: '1606778597183'
  alias: Host Offline
  description: Pc offline
  trigger:
  - platform: state
    entity_id: binary_sensor.desktop_onoffline
    from: 'on'
    to: 'off'
  condition: []
  action:
  - service: script.pc_50
    data: {}
  - service: switch.turn_on
    data: {}
    entity_id: switch.test_o2
  mode: single

n°2

- id: '1605190998002'
  alias: B/M off
  description: B/M off
  trigger: []
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: media_player.living
      state: playing
    - condition: device
      type: is_on
      device_id: 'id'
      entity_id: light.l1
      domain: light
  action:
  - delay:
      seconds: "{{ 0 if state_attr('sun.sun', 'elevation') >= -5 else 10 }}"
  - type: turn_off
    device_id: 'id'
    entity_id: light.l1
    domain: light
  - delay: 00:01:00
  - service: media_player.media_pause
    data: {}
    entity_id: media_player.living
  mode: single

EDIT: re-labeled automations

I have a few doubts about this automation. I believe you have mislabeled n°1 and n°2 in the previous post and first post. Secondly Is the dimming if light executed with script.pc_50. And also why is there no trigger for n°1?

That trigger (now n°2) is a lovelace switch.

I think first you should create an input_boolean. In the n°2 automation add the action to switch on this input_boolean as the first action and at the last action to switch off the input_boolean. In the n°1 automation put a condition that this input_boolean is off to execute. So in this case if the automation two has executing, the input_boolean will be turned own until the automation is finished and in the mean time if the n°1 automation runs, it will not execute as the input_boolean is on. You can also use a delay entity at the last part of n°2 to extend switching off the input_boolean.

1 Like

Thanks for this idea @sheminasalam since you are teaching me something new!
I succeeded to create this input_boolean and added it as you suggested in automation n°2.
It also solves another problem I had with a conditional card, so BIG thank you!

I get the bigger picture but I don’t understand (yet) the implementation.
n°1 is always executed first; I don’t leave without switching off my pc.
So it’s that script ‘script.pc_50’ that gets executed after I run n°2

With your solution it seems like n°2 is the ‘main’ automation, or am I wrong?

Thanks a lot for putting me on the right path @sheminasalam; I think I have solved it.

In n°2 I have used that input_boolean as a state for being in the room so I turn it off as the first action.
In n°1 I use this state as a condition, after swapping the order of the actions, to determine if the script to dim the light has to run (or not). By swapping the actions and putting the light action at the bottom linked to the condition, that part is not executed.

When entering the room again, I have another automation which sets that input_boolean back to on.

Happy to help