(How to?) Pass "variable" between automations

Hello community, is it possible to pass variable from an automation to another? I have 2 automations working perfect with Xiaomi motion sensor, something like:

1st automation: if motion = on then turn light on
2nd automation: if motion = off then turn light off

I prefered to split them because there are a lot of conditions into these automations. Well, what I want to accomplish is the following. Switch off the lights only if the lights are ON because triggered by 1st automation. This because, if I use the switch to manually turn on the lights I don’t want the 2nd automation turn off the lights. So something like:

1st automation:
If motion = on then
    turn light on
    lightmotion=1

2nd automation: 
if motion = off and lightmotion=1 then 
     turn light off
     lightmotion=0

I report mine automations here just for references:

1st (switch on the lights on motion)
- id: movimento_accendi_luce_sala  
  alias: Accendi Luce Sala Movimento  
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxxxxxx
    from: 'off'
    platform: state
    to: 'on'
  condition:
    - condition: and  
      conditions:  
        - condition: state  
          entity_id: light.yeelight_ceiling4_mibt77360569_miio_xxxxxxxxxxxxxx  
          state: 'off'  
        - condition: state  
          entity_id: light.yeelight_ceiling4_mibt77361672_miio_xxxxxxxxxxxxxx 
          state: 'off'  
        - condition: numeric_state
          entity_id: sensor.illumination_xxxxxxxxxxxxxx
          below: 100 
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.yeelight_ceiling4_mibt77360569_miio_xxxxxxxxxxxxxx, light.yeelight_ceiling4_mibt77361672_miio_xxxxxxxxxxxxxx
      brightness: >
        {%- if now().strftime('%H')| int >= 21 %}
          100
        {%- elif now().strftime('%H')| int <= 7 %}
          10 
        {%- elif now().strftime('%H')| int >= 9 %}
          255
        {%- endif %}
2nd (switch off the lights on no motion)
- id: no_movimento_spegni_luce_sala
  alias: Spegni Luce Sala No Movimento
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxxx
    platform: state
    from: 'on'
    to: 'off'
    for:             
      minutes: 3
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.motion_sensor_xxxxxxxxxxx
        state: 'off'
      - condition: or
        conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_mibt77360569_miio_xxxxxxxxxxx
            state: 'on'
          - condition: state
            entity_id: light.yeelight_ceiling4_mibt77361672_miio_xxxxxxxxxxx
            state: 'on'
  action:
    - service: light.turn_off
      entity_id: light.yeelight_ceiling4_mibt77360569_miio_xxxxxxxxxxx, light.yeelight_ceiling4_mibt77361672_miio_xxxxxxxxxxx
    - service: tts.google_say
      entity_id: media_player.camera_da_pranzo
      data:
        message: >                   
          Ho spento le luci della sala per mancanza di movimento

Thanks all
Lucas

1 Like

You should be able to set an input boolean with the first automation, and check that on the second automation (only run when input boolean is on) and at the end set the input boolean to off again?

are you trying to figure out if the light was turned on from the motion? I ask this because you can make an automation only fire from another automation, it just takes some script work.

can you explain your automations a little more?

I think he only wants the second automation to run if the lights are turned on by the 1st automation(motion)

He does not want thr lights to turn off if the switch has been used

My whole automations is on the first post :slight_smile:

Correct! Firstly I need to understand how to do that in theory, then how to apply it with the code!

This is perfect, but how to apply it? :thinking:

You need to create an input_boolean:
https://www.homeassistant.io/components/input_boolean/

After that you can create an action in your automation to turn it on

On the second automation you create the condition to only run when the input boolean is “on” and at the end of the automation create an action to turn the input boolean “off”

1 Like

Right, I understand that but how does your motion work? Motion is typically an on -> quick off. So usually, if you want the light to stay on a bit, you need to build some scripts. This is what I did with my motion detection which is 100% independent from normal day to day use:

automation:

- alias: Hall Motion Detected During Night
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_ms_sensor_32_0
      to: 'on'
  condition:
    - condition: time
      after: '23:00:00'
      before: '07:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hall_motion_night

scripts:

alias: Hall Motion Night
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.hall_light_timer
  - service: light.turn_on
    data:
      entity_id: light.hall_d_level_10_0
      brightness: 10
      #brightness: "{{ 255 * 0.15 | round(0) | int }}"
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.hall_light_timer

alias: Hall Light Timer
sequence:
  - delay:
      minutes: 5
  - service: light.turn_off
    data:
      entity_id: light.hall_d_level_10_0

What happens is, the motion turns the light on and starts a timer. If motion is detected again, the timer resets. Once the timer runs out, it turns the light off.

No need for extra booleans or anything.

2 Likes

petro, thank you for post your code, but imagine this. You enter into living room, then the light/s turn on due to motion. That’s ok. Then you sit on the couch to read a book, so no motion, and BAM! The 2nd automation turn off the lights. This is because I want the 2nd automation is disabled if the lights are not powered on by motion. And btw, my script works like yours, it waits 3 minutes after no motion before turn off the lights! :wink:

Damn! Maybe it’s my fault, but the page doesn’t load. Btw, I’ll search the net for input boolean references, looks promising

Edit, this is the right address :slight_smile:

Let’s see if I can do what I need, any other advices are welcome anyway!

I have a similar problem, I haven’t taken the time to fix it, I have a page where all my automations can be switched on and off so I manually switch off the ‘auto switch off’ automation.

So I was wondering, if your light switch was a smart switch it could (as well as turning on the light) turn off your secondary automation?

I have several of these around my house. Mostly for transient motion (not living room or bedroom or whatever), more like hall/stairs/driveway lights. My PIR sensors are MQTT so here is how they work:

Motion detected–MQTT message sent, automation turns on the light
After 2 minutes (or whatever interval) with no “new” motion, the timer that I started with the original motion turns the light off.
If the motion continues, the timer gets reset continuously (keeping the light on)

I don’t use a boolean and I don’t pass variables between automations

CAVEAT: Its very easy to be in a living area and not generate enough motion to be detected. This is why I don’t have any in my living areas etc.

Yep, that’s the implementation I supplied.

1 Like

@ericleejoe But that would mean he’d have to keep waving at the sensor whilst reading his book to keep the lights on.

@petro. Yes :D. I was just saying, I do this too and it works fine.

1 Like

Yes. I said that. That is why I don’t do it in my living area.

You could add an action to your “Hall Motion Detected During Night” automation to turn on your “Hall Motion Night” automation. Then in your “Hall Motion Night” automation, you could add an action to turn itself off. This can be accomplished with the

action:
  - service: automation.turn_on
    entity_id: automation.hall_motion_night

Yes, this is the point. However I’m implementing an input boolean switch, it’s a dirty solution, but should works. Now I have to figure out how to set the input_boolean into automation

action:
     service: input_boolean.turn_on
     data:
       entity_id: input_boolean.ejhome

So you are going through all this effort to have an automation to turn off, but i think its not going to work the way you think it is. It’s going to turn off when the motion turns off because you are still basing the off-automation off the motion… The input boolean is just going along with the ride, and you are turning that on/off with the motion.

In the end, your automation:

if not motion and input_boolean:

will be the same as

if not motion:

Not properly true. I have 2 Yeelight Ceiling light in my living room, and actually I can turn on them in several way:

  • Using Google Home (Voice command)
  • Using Wireless Switch (Xiaomi)
  • Using its remote (BT)

So, tried just know, in case I use the above methods, the lights are not turned off by automation. But, and this is the jolly :smiling_imp:, if, for any reason you’ll move, the Motion Sensor is not triggered because there is a condition who say: if illumination value is above 100 (so light is on) disarm the motion sensor :smiley:

Lol nice, ya that should work. I can only see issues if you are just passing through the room and want the lights to shut off after no motion.

Someone just needs to develop presence detection that doesn’t require movement or a cell phone/fob.