I have this set of two automatons that work and i have another 6 sets of similar ones. They take up a lot of space and i was wondering if it was possible to combine each set into 1 automation.
#Turns on landing lights when motion
- alias: Landing light motion on
trigger:
platform: state
entity_id: binary_sensor.landing_motion
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: light.landing_light
#Turns off landing lights after 2 mins when no motion
- alias: Landing light motion off
trigger:
platform: state
entity_id: binary_sensor.landing_motion
to: 'off'
for:
minutes: 2
action:
- service: homeassistant.turn_off
entity_id: light.landing_light
i thought something like this but don’t know how to code the delay. is it possibel without setting up a seperate script?
- alias: Landing light motion on and off
trigger:
- platform: state
entity_id: binary_sensor.landing_motion
to: 'on'
- platform: state
entity_id: binary_sensor.landing_motion
to: 'off'
for:
minutes: 2
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
entity_id: light.landing_light
Is there any uniformity in how they are named? For example, you have landing_motion and landing_light. They have ‘landing_’ in common. Do the others share this format where a common word precedes either motion or light (separated by an underscore)?
If they do then there’s an opportunity to uses Phil’s example to control all of them (i.e. one automation to rule them all).
so i have these other sets of automation’s. im assuming i could combine the 1st group into 1 automation as you suggest but no idea how to do it.
1st group
#Landing motion light
- alias: Landing light motion
trigger:
- platform: state
entity_id: binary_sensor.landing_motion
to: 'on'
- platform: state
entity_id: binary_sensor.landing_motion
to: 'off'
for:
minutes: 2
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
entity_id: light.landing_light
#kitchen lights when motion
- alias: Kitchen light motion
trigger:
- platform: state
entity_id: binary_sensor.kitchen_motion
to: 'on'
- platform: state
entity_id: binary_sensor.kitchen_motion
to: 'off'
for:
minutes: 10
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
entity_id: light.kitchen
#dining lights when motion
- alias: dining light motion
trigger:
- platform: state
entity_id: binary_sensor.dining_motion
to: 'on'
- platform: state
entity_id: binary_sensor.dining_motion
to: 'off'
for:
minutes: 10
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
entity_id: light.dining
then different but still combine-able now with phils code
2nd group
#Turns on/off guest light with door
- alias: Guest light
trigger:
platform: state
entity_id: binary_sensor.guest_door
to: 'on'
action:
- service: homeassistant.turn_on
entity_id: light.guest
#Turns off guest light when door closes
- alias: Guest light door close
trigger:
platform: state
entity_id: binary_sensor.guest_door
to: 'off'
action:
- service: homeassistant.turn_off
entity_id: light.guest
and then 3rd group
#Turns off lamps when shield is playing
- alias: shield Lamps off
trigger:
platform: state
entity_id: media_player.shield
to: playing
action:
- service: homeassistant.turn_off
entity_id: light.lamps
#Turns on lamps to 1% when shield stops playing
- alias: shield Lamps on
trigger:
platform: state
entity_id: media_player.shield
from: playing
action:
- service: homeassistant.turn_on
entity_id: light.lamps
data:
brightness_pct: 1
#Hall lamp on
- alias: Hall lamp on
trigger:
platform: sun
event: sunset
offset: "-00:45:00"
action:
service: homeassistant.turn_on
entity_id: light.hall_lamp
#Hall lamp off
- alias: Hall lamp off
trigger:
- at: '00:00'
platform: time
action:
- service: light.turn_off
entity_id: light.hall_lamp
I’v been trying to learn. You managed to condense my original post from 2 automation to 1 but i should have said i has 2 other sets of 2 automation for 2 different rooms doing the same things. would the following 2 automatons work to turn on/off the lights independently in each of my 3 rooms?
- alias: experiment - motion lights on
trigger:
- platform: state
entity_id:
- binary_sensor.kitchen_motion
- binary_sensor.dining_motion
- binary_sensor.landing_motion
to: 'on'
action:
- service: light.turn_on
data_template:
entity_id: "{{ trigger.entity_id }}"
- alias: experiment - motion lights off
trigger:
- platform: state
entity_id:
- binary_sensor.kitchen_motion
- binary_sensor.dining_motion
to: 'off'
for: '00:10:00'
- platform: state
entity_id:
- binary_sensor.landing_motion
to: 'off'
for: '00:02:00'
action:
- service: light.turn_off
data_template:
entity_id: "{{ trigger.entity_id }}"
Tinkerer
(aka DubhAd on GitHub)
Split this topic
10
Close, but not really. trigger.entity_id is the entity_id of the entity that caused the automation to trigger. But those are binary_sensor’s, not lights. So you’d be trying to use the light turn_on/turn_off services to turn binary_sensors on or off, which of course would not work.
Given what you provided earlier, I think you can handle all of them in one automation. What would make it much easier is if you changed light.landing_light to light.landing. I’ll assume you do this. Then you can do this:
- alias: experiment - motion lights on
trigger:
- platform: state
entity_id:
- binary_sensor.kitchen_motion
- binary_sensor.dining_motion
- binary_sensor.landing_motion
to: 'on'
- platform: state
entity_id:
- binary_sensor.kitchen_motion
- binary_sensor.dining_motion
to: 'off'
for: '00:10:00'
- platform: state
entity_id:
- binary_sensor.landing_motion
to: 'off'
for: '00:02:00'
action:
- service_template: "light.turn_{{ trigger.to_state.state }}"
data_template:
entity_id: "light.{{ trigger.to_state.object_id.replace('_motion','') }}"
If there’s uniformity and commonality in the naming of motion sensors and corresponding lights, a template can easily parse their names (without any undue complexity).
Yes but, like I stated earlier, there’s an opportunity to use Phil’s one automation to rule them all. It all depends on how you have named the entities … and if you’re willing to rename them in order to take advantage of that opportunity.
If the naming follows a fixed, consistent pattern like location_function (e.g landing_motion and landing_light) then the template has an easy time of manipulating them (see Phil’s example above).
Just be aware that there has been a bug in the for: option that will prevent this from working correctly in all situations. However I fixed that bug and the fix is in 0.96.
but I’m having trouble trying to make 1 automation to turn on/off lamps for my shield depending on playing state. I want it to switch off when it starts playing and switch on when paused/idle but to 1% brightness - strugling to add data into the automation.
#Turns off lamps when shield is playing
- alias: shield Lamps off
trigger:
- platform: state
entity_id: media_player.shield
to: playing
- platform: state
entity_id: media_player.shield
from: playing
action:
- service_template: >
{% if is_state('media_player.shield', 'playing') %}
light.turn_off
{% else %}
light.turn_on
{% endif %}
- data_template: >
{% if is_state('media_player.shield', 'playing') %}
light.turn_off
{% else %}
light.turn_off
{% endif %}
entity_id: light.lamps
these work below
#Turns off lamps when shield is playing
- alias: shield Lamps off
trigger:
platform: state
entity_id: media_player.shield
to: playing
action:
- service: homeassistant.turn_off
entity_id: light.lamps
#Turns on lamps to 1% when shield stops playing
- alias: shield Lamps on
trigger:
platform: state
entity_id: media_player.shield
from: playing
action:
- service: homeassistant.turn_on
entity_id: light.lamps
data:
brightness_pct: 1
hi ,
I’m following your advice,
this is my automation, do you think should work?
- alias: Luce muro esterno on and off su movimento
trigger:
- platform: state
entity_id: binary_sensor.0x000b57fffe91ee74_occupancy
to: 'on'
- platform: state
entity_id: binary_sensor.0x000b57fffe91ee74_occupancy
to: 'off'
for:
minutes: 2
condition:
condition: time
after: '01:00:00'
before: '06:00:00'
action:
- service_template: "homeassistant.turn_{{ trigger.to_state.state }}"
entity_id: switch.luci_muro_esterno
- delay: '00:00:02'
- service_template: "homeassistant.turn_{{ trigger.to_state.state }}"
entity_id: switch.parentesi