If you can find the history of these triggers and provide me the trace log, it will be easier to find out.
It depends on if you only need to see the log, or you actually want to create another automation based on it.
If you only want to see the log, you can find the entry in logbook telling you if the light is turned on/off by an automation or anything else.
If you want to create another automation based this automation, you can create a script, then put the script in the “target turn on/off action” instead of “homeassistant.turn_on/off”. In the script, you can turn on/off the light, meanwhile do something else meaningful to you.
Does your automation require both motion to clear and the door to close to timeout? I am looking to make a similar automation for bathrooms but in my case the doors do not always get closed when someone leaves. I am trying to turn the light on with either door opening OR motion is detected, but only want to turn off based on motion (not requiring the door to close for the timeout).
I thought maybe I could do this with groups but I think I would either basically need a group that turns on with either the door or motion but then stops following the on state of the door, or I’d need to modify the script to have different trigger entities for on and off. Any thoughts on if there is an easier solution here I am maybe just not thinking of?
Hi Ryan,
This automation does require the “trigger entity” to be clear before turning off the target (light in your case). So, if you want to separate treat door and motion differently, you need to use some helpers. You can pipe multiple automations through helpers, which is supported by this automation.
You can create a “input text” helper. For example, assume you created an input_text helper named door_motion
, then its ID will be input_text.door_motion
. After that, you can create another automation to tweak this input_text helper to make this automation work. You can use different values of this door_motion
to represent the states of doors and motion sensors. For example, value door_on_motion_off
means at least one door is open, but the motion is off; value door_off_motion_on
means doors are closed but motion is on.
Under that assumption, you can now create a group contains all the doors, then another group contain all the motion sensors. Then create a new automation which will set the value of the input_text.door_motion based on the states of the groups. It can be as simple as the following:
alias: Set Door Motion State
description: ''
trigger:
- platform: state
entity_id: group.doors
- platform: state
entity_id: group.motions
condition: []
action:
- service: input_text.set_value
target:
entity_id: input_text.door_motion
data:
value: >-
door_{{ states('group.doors') }}_motion_{{
states('group.motions') }}
mode: single
It basically sets the input_text.door_motion
whenever the states of doors or motion sensors are changed.
Now, after you tested the above automation works, you can go back to this automation and set the trigger_entity_state
and trigger_entity_state_off
accordingly, both of them support regular expression.
In your case, trigger_entity_state
could be door_on_motion_off|door_on_motion_on|door_off_motion_on
, which means the light should turn on either the door or motion sensors are on. Then trigger_entity_state_off
could be door_(on|off)_motion_off
, which regardless of the state of the door, turn the light off when motion sensors are off.
Regarding your second question, this automation accepts script as target_entity
. If you put your script ID there, it will execute that script. Fill the target_entity
with your script’s ID, then set target_entity_state
to none
, the automation will then only honor the trigger entity’s state. Optionally, you can also tweak target_entity_action_on
and target_entity_action_off
to fully customize this.
You can also set target_entity_off
if you are using another script to turn off the lights. Or simply put the light group’s ID there.
Hopefully I explained it well. You should also find the description of each fields in my automation helpful.
@kevinxw: At one single point your blueprint uses int
without setting a default which gives errors in HA log:
Template warning: 'int' got invalid input 'unavailable' when rendering template '{{ states(illuminance_sensor or '') | int | default(99999, true) <= illuminance_below | int(-1) or ((sun_elevation_below or sun_elevation_below == 0) and state_attr('sun.sun', 'elevation') | int(91) <= sun_elevation_below | int(-91)) }}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.6
The interesting thing is the last part:
...however this template will fail to render in Home Assistant core 2022.6
.
That single point is in line 365 of https://github.com/kevinxw/homeassistant/blob/55fa2a2867292dd44f7b631daf5d476345019c6e/blueprints/state_based_entity_control.yaml: ...nsor) | int | default(99999
What is the right default here? int(0)
?
Thank you for clarifying and updating the blueprint.
Hi Kevin. Just now seeing this but thank you so much for this example. This is perfect and exactly what I was needing. I never even considered using helpers in the manner you described but it makes total sense now. Really appreciate it.
If I may pick your brain on one other scenario, I am curious if there is a more elegant solution to addressing the other aspect of this automation I am looking to customize. The wife was not a huge fan of lights coming at whatever brightness they were last left at during late night hours when I first deployed the automation. So I originally took the approach of leveraging helpers to manage a “night lights” setup:
input_datetime.night_light_start_time
- configurable start time for nighttime
input_datetime.night_light_end_time
- configurable end time for nighttime
input_number.night_light_brightness
- configurable brightness for lights coming on during nighttime
Then I started creating a script per room/automation that is called by the blueprint rather than home_assistant.on. This works but creates a lot of extra overhead managing all these scripts. Is there possibly a way for the blueprint to turn on the target entity to a specific brightness value in the blueprint from an input_number? (I would use other automations to control setting something like input_number.light_brightness to the proper brightness based on the chosen times in the input_datetime values).
Separately could I leverage the value of something like input_number.light_timeout in the trigger timeout section? I tried this using "{{ states("input_number.light_timeout") | int }}"
as the trigger timeout but it did not like that at all, the automation entirely disappears and I have to remove it from the automations.yaml file manually.
If I could figure out how to leverage values of helpers in other areas of the blueprint like Trigger Timeout, Alternate Trigger Timeout, etc. I could get really flexible with this using helpers based on what I learned from your previous example.
Hopefully this makes sense. Ultimately the goal here is to leverage this awesome automation but still allowing others in the house to configure brightness/timeout/time ranges to their liking.
Hi, im trying to turn on mood lighting outside (Philips Hue) and then when motion is detected outside 2, turn on all the lights to full brightness and 3, after X time revert to previous state (scene or turn off). Would this work?
Hi, i am trying to move back to yaml with my motion automation. Before I’ve used entity-controller.
I’ve tried several blueprints and I like your one. Thank you for your work.
But I have a question: Is it possible to add the following features?
- Wait time: Time to wait before firing the next action after motion trigger, so that you can leave the room.
* Only run after time
* Only run before time(Found it after updating the blueprint)
You will have to use another script the set the brightness of the light. The automation can only control when to turn on/off the light. You can let this automation to run your script the set the brightness based on conditions you specified.
You mean if the motion trigger detects nothing, then it will turn off the light, but before that happens, you want some wait time?
Not sure if I got you. Won’t the motion trigger again when you are leaving the room?
Oh, maybe I didn’t describe that very well either. Sorry. I know this from other automations like this: ha-node-red-flows/subflows at master · dortamur/ha-node-red-flows · GitHub
If a light is manually changed to be off, presence events are ignored for a number of seconds
This gives you the possibility of switching off the light manually leaving the room and without a simultaneous trigger triggering the automation again.
Got it. It is possible, however, not in an easy way. The home assistant automation doesn’t maintain the state of its triggers, which means when a light is turned on, we do not know if it’s turned on manually or it’s turned on by another automation. I think node red flow is capable of maintaining the state.
We can leverage a helper input to store the state, but the automation will not be straightforward as it is right now. I will see if I have time to do this, but no guarantee.
So I’m trying to solve a similar situation. I think my approach is going to be to use Scene Control on my ZWave switches so that a double-press off the light switch will disable this automation for X seconds.
Hi, I have the following case:
Trigger: Sensor with numeric Output (Power in W delivered by PV)
Target: A simple switch to get on or off dependent on the current power measure of the trigger.
Need something like:
if trigger_entity below x => Target off
if trigger_entity above y => Target on
Can I do this using this BluePrint?
If yes, how to configure the parameters?
If no, is there already another BluePrint fulfilling my goal?
Also just wanted to add, if anyone else happens to be looking for an elegant way to pair this automation with automated brightness control like I was: I’ve been using this blueprint along with the Adaptive Lighting integration (https://github.com/basnijholt/adaptive-lighting) and I’ve been super happy with the results. I don’t have to have numerous complex scripts/automations for each set of lights to handle it all. I just have this blueprint running per room/set of lights and those same lights together in an instance of Adaptive Lighting and it works fantastically together.
For some reason automation based on this blueprint stopped working. It turns the light off in a few seconds after turning it on. I believe there may be some changes in Home Assistant which caused such behavior. Please help
Are you sure something didn’t change with your triggers/sensors to cause that? Fwiw I have over a dozen automations using this blueprint and running latest HA and they are all still working just fine. Maybe the log of the target light can tell you what turned it off? If it is the automation then maybe a examining the trace?
Hello!
I would like to turn on bulbs ans switches but I read it cannot handle multiple target_entity.
Is the creation on 2 automations (one for bulbs and one for switches) a solution?
Thanks!
I’m having the same issue with input numbers.
The automation stops working when I use an input number.
This is not working:
alias: Light On Motion - Hal
description: ""
use_blueprint:
path: kevinxw/state_based_entity_control.yaml
input:
trigger_entity: binary_sensor.motionsensor_hal_occupancy
target_entity: light.hal
illuminance_sensor: sensor.motionsensor_hal_illuminance
illuminance_below: input_number.hal_illuminance_threshold
This is working:
alias: Light On Motion - Hal
description: ""
use_blueprint:
path: kevinxw/state_based_entity_control.yaml
input:
trigger_entity: binary_sensor.motionsensor_hal_occupancy
target_entity: light.hal
illuminance_sensor: sensor.motionsensor_hal_illuminance
illuminance_below: '10'
I figured this out myself in the meantime so I’m leaving this here in case someone else runs into this issue:
The helper I created in the UI is having a decimal value. (For example ‘10.0’)
I used a template to get the integer value for the input_numer and now it works.
alias: Light On Motion - Hal
description: ""
use_blueprint:
path: kevinxw/state_based_entity_control.yaml
input:
trigger_entity: binary_sensor.motionsensor_hal_occupancy
target_entity: light.hal
illuminance_sensor: sensor.motionsensor_hal_illuminance
illuminance_below: "{{ states('input_number.hal_brightness_threshold') |int }}"