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
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.
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â
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.
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!
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
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.
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
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
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.
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 , 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