Execute any service (turn on switch/light/scene/scripts etc., even run another automation) based on any entity's state change (motion sensor, illuminance, sun rise/set)

Hey, I updated the blueprint several times after Feb 2nd, the latest version is at Feb 26. Sorry I didn’t post update on the forum since there was few replies. Try the latest one using the URL from main post, ideally there should be no problem.

trigger_timeout supports seconds and the official time format “HH:MM:SS”, thus it’s a “input_text”. If you adjust the domain it should work.

For multiple trigger entities, this blueprint supports it. I do not have vibration sensor, but I do have door contact sensors and motion sensors, and I do have an automation combining contact sensors and motion sensors. The trick is to create a group with these sensors. I am not very clear what the group’s status will be if you put a vibration sensor there, you can test it by checking the group’s state in developer tool. For group containing only contact sensors and motion sensors, the group’s status will be just on/off.

I have the following group in groups.yaml

garage_motion_door_sensors:
  name: Garage Sensors
  entities:
    - binary_sensor.motion_sensor_158d00020b8046
    - binary_sensor.motion_sensor_158d000116ca1c
    - binary_sensor.door_window_sensor_158d0001a2289f

And an automation like this:

alias: Garage Light
use_blueprint:
  path: kevinxw/state_based_entity_control.yaml
  input:
    trigger_entity: group.garage_motion_door_sensors
    target_entity: switch.garage_light
    illuminance_sensor: sensor.illumination_7811dcdc2cde
    illuminance_below: '450'
    trigger_timeout: '90'

It turns on light when any of these motion sensors detected something or the door is open, and turns off light when everything goes quiet.

1 Like

Hi Kevin

This is really cool and super-helpful!! I am trying to setup a variation - to make a decison on the sum of 3 values subtracted from another. I have tried to use a Helper but figured your blue print could be the solution.

I am on a remote island in the Bahamas and I have a solar array that have intergated as
sensor.power_solar_production_1_1min

And depending on how much power my kitchen and boats are using, I want to control how much power I send to my chargers. I have 1.8kW, 3.3kW and 6.kW that charge my 15kWh, 30kWh and 75kWh battery packs (all ex-Tesla Model S and 3 from crashes)
My chargers are all controlled through Shelly1 relays.

So I need to calculate

   sensor.power_solar_production_1_1min - sensor.power_kitchen_panel_consumption_6_1min - sensor.power_shore_power_1_7_1min - sensor.power_shore_power_2_8_1min

And then trigger the different charge relays

  switch.18kw_charger_relay
  switch.33kw_charger_relay
  switch.66kw_charger_relay

I realize this is a little nuts and unique - and to top it off I only started using HA three weeks ago so I am stumbling around like a drunken sailor. I don’t want to step on anyone’s toes with stupid questions (I likely have), so please forgive me if this is the completely wrong place to post this question!

Thanks
Wayne

Hey Wayne,
That sounds like an interesting project. However, it’s not a fit for this blueprint. This blueprint is only suitable for binary switches (on <-> off etc.). But I have another blueprint may be helpful to you. I use this blueprint to convert my 30 bucks heater to an air conditioner.

The blueprint is at: https://github.com/kevinxw/homeassistant/blob/61b5859dc866edd43da8f5e7cac244e9fbda00b0/blueprints/control_entity_using_sensor_with_numeric_states.yaml
Its concept is simple, when the temperature returned by a climate sensor is above certain value, it turns off the heater; when the temperature is below certain value, it turns on the heater. But the blueprint does not limit itself to heater. You can use it for any sensor that returns a numeric state. I also use it for adjust my humidifier.

From what I understand, you need to create a custom solar power sensor using template. You can find examples here: https://www.home-assistant.io/integrations/template/, you should put the equation you created above into its value_template, so you can rely on that sensor to output a number.

Then, use the blueprint I mentioned above to create 3 automations, which turns on/off your chargers based on the value returned by the sensor you defined above. e.g., when solar power sensor returns a value below 1800 (I suppose the sensor you created will return watt number), turns on switch.18kw_charger_relay, when it’s above 1805 (give some buffer, otherwise the switch may be flapping), turns off switch.18kw_charger_relay, then in the second automation, do the similar but with 3300 as threshold. Then in the third automation, use 6600 as threshold.

I will be interested to see it working.

Awesome - thanks Kevin. Will give it a shot. Hey, if you are ever around the Abaco’s, you are welcome to stop by - it is really beautiful and also a tech paradise. I have built everything myself - I believe I was the first person in the US to take an entire 75kWh Model 3 battery pack and turn it into solar storage. I “fooled” two Danfoss DLX inverters (4.4kW each) into seeing it as a 400V solar field :smile:

I am still very weak at HAss, but trying to get up to speed quickly.

1 Like

Bug in newer versions:
Nope, does not work with latest version. Needed do go back to the old one. Seems like there´s a bug introduced in any newer version. I don´t care a lot currently, still working on tuning the old one to fulfill all requirements.

Multiple trigger entities:
That´s a workaround you use. Can you update the blueprint to simply allow multiple trigger entities? I know how to basically do this in an automation, but no idea how to adjust your blueprint… :frowning:

Your workaround using group entity has a big downside: what if you want to control more than “just” your garage light? Adding another group entity combining all target entities? Setting up a scene? That´s just too much group entity creation considered that an automation is capable of handling the same. Would really love to see this in your blueprint! :slight_smile:

So the main difference is:

  • you use this blueprint for ACTION - to do something based on presence (indicated by a presence group)
  • i use this blueprint for DETECTION - (one to get information from motion sensor, one from vibration sensor - makes 2 automations) to set a presence helper entity, which triggers a separate manual automation doing different things (more than only switching light on)

By being able to select multiple trigger entities this blueprint would not only fit my purpose, it would also allow a much broader usage of it. Ultimate solution would be to being able to set multiple trigger as well as multiple target entities.

What do you think?

OK forget about it, I changed my whole logic and I´m using a group of trigger entities too now.

What I really miss now to replace all my automations based on freakshock88´s blueprint is something he seems to have added after you forked it: a time range condition:

grafik

That´s really the only thing I miss. Can you add this to your blueprint? I really like the broader approach. Time range condition really is the only thing left compared to the original :slight_smile:

Regarding the bug, what exactly is not working?

Multiple trigger entities will not be supported due to the limitation of a blueprint. It only support fixed number of trigger entities. Group is the way to go. If you want to turn on/off multiple entities, create a group for that as well. Everything in that group will be turned on/off.

Yeah as mentioned I got that and changed my whole setup alreay :slight_smile: Last two cents:

  1. I´ve seen another blueprint where using several entities with as special syntax (don´t remember, either “entity1,entity2” or “entity1|entity2” was used, so it is possible.
  2. If one of that actions to be applied to a group of target entities is not just on/off but e. g. “set temperature to XX °” the group entity thing comes to an end quite quickly. BUT as this blueprint is quite general, one could use a script instead which takes care of such extra tasks.

See Execute any service (turn on switch/light/scene/scripts etc., even run another automation) based on any entity's state change (motion sensor, illuminance, sun rise/set) - #16 by e-raser that´s what I experienced, but I´ll update and test again. As it seems to work for other users, it might be a me specific issue.

@kevinxw: What about Execute any service (turn on switch/light/scene/scripts etc., even run another automation) based on any entity's state change (motion sensor, illuminance, sun rise/set) - #22 by e-raser ?

I have pushed an update to my blueprint. You can reimport it now. It adds a field letting you specify Execution Time Ranges

If you can show me the link of the blueprint that supports multiple trigger entity, I can probably support that as well.

My automations are running fine with the latest version for 1+ months now. I will be happy to debug if you can provide more details (logs and automation yaml etc.)

Finally I could update to the latest blueprint version and test it. Summary:

  • runs fine without any issues so far
  • could not test the Execution Time Ranges yet (as I added illuminance sensors meanwhile I tend to not use them for now, but there will be situations I´ll have to rely on time instead of illuminance so it´s great to have this option!)
  • really love the updated Alternate Trigger Timeout syntax, very powerful and easy to use

So far: really love that blueprint. Outperforms all similar I´ve seen here and tested (some for weeks) because I like having all configuration in the automation. Other blueprint authors tend to select entities which bloats the user interface or at least you need to create many helper entities.

Regarding

I can say it wasn´t a blueprint I´ve seen, instead it was a regular syntax in a normal automation (trigger section):
grafik

I´m happy so far and will test the updated blueprint for some while. Thank you so much!

I didn’t know home assistant accept this format. It seems very straightforward. I will support multi-entity trigger later.

It looks like the automation does not work without any conditions.
For one instance of blueprint I need just a simple motion sensor trigger → light on.
If I add sun elevation parameter the light starts triggering, but without any additional parameters it does nothing

I have fixed the logic.
If you update the blueprint to the latest version, you can leave both luminance sensor and sun elevation blank.

The latest blueprint supports multiple trigger entities and target entities now. You can test it if you are still interested.

I could not test it yet, but thanks in advance!

Meanwhile I encountered a daily issue in using this blueprint. I‘m curious to see if someone smart has an idea on how to fix this:

I use this blueprint to have a motion sensor turning on a light in a room, leaving it on for some time and turning it off. For this my automation looks like:


use_blueprint:
    path: kevinxw/state_based_entity_control.yaml
    input:
      trigger_entity: binary_sensor.motion
      target_entity: light.lamp
      illuminance_sensor: sensor.illuminance
      illuminance_below: '65'
      trigger_timeout: '90'

Works just perfect. BUT: I have an additional switch for manually controlling the light. Sometimes I get in the room - automation turns light on and off after a while. Without triggering the motion sensor again, I turn the light on manually using the switch because sometimes I need the light for a part of the room (where motion sensor is monitoring, that’s desired).

Expected behavior:
light is turned on and stays on.

Actual behavior because somehow this automation is being triggered:
Light turns on BUT ALSO AUTOMATICALLY OFF after the time set in the automation. This also happens when switching the light on using Home Assistant (Lovelace GUI). Motion sensor never is being triggered.

Is this what the blocking entity is made for? How can I adjust this automation so that if the light is being turned on manually (using the switch) it is not being turned off?

In other words: why is the state of my TARGET entity being used for triggering the automation? I only want the automation to trigger when the TRIGGER entity changes state. Bug? Please assist.

This is expected. It’s kind of a feature. I want the light to be automatically off even when I manually turned on the light.
If you want to ignore target entity’s state, set “target_entity_state” to empty string.

I wanted to implement the feature that when a person manually turn on the light, leave the light on. However, there is a limitation which I currently do not have any solution. Home assistant doesn’t tell me the source of event when a light is turned on, i.e, I cannot tell if it’s turned on by someone physically touching the switch or if it’s turned on by another automation.

I concur with what Kevin said but you do have some options if you are keen for a more manual setup.

I have e.g. a front door light that turns on automatically when someone gets home. It also has a manual switch. When the first automation fires (getting home), it separately starts a timer. When that timer expires, there’s a second automation to turn off that light. This timer won’t start when the light is turned on manually (because there’s no automation on that state change). You’re problem is, of course, that you could forget to turn off that light (generally, maybe not in your case). For that you can have yet another automation to notify you if the light is still on after some time. And so forth…

I did for all my automations, but it didn´t change anything unfortunately. Automation still gets triggered and turns off the light, even when I turned it on manually before.

Update: GUI has some issues, why removing the “on” value has not been saved as expected. I had to manually edit in YAML mode. Is this the right value (no value at all)? The help text talks about ‘none’ as value :slightly_smiling_face:

target_entity_state: ''

@parautenbach: That would of course be a solution, a quite smart one. BUT also pretty oldschool, cause creating various automations for controlling e. g. one light is a path I´m coming from. I want everything in ONE automation, which is why I think this blueprint is just perfect for.

I´m still looking for a solution to have my lights NOT turned OFF when I switched them on manually (not by motion sensor used as trigger entity in this blueprint based automation) - by only using this single blueprint automation.

Just an idea: can´t you check the last_triggered time of the blueprint automation? This way you still don´t know why/which way e. g. the light has been turned on, but you know it wasn´t the automation. Should be enough piece of information to make some progress if I don´t miss or mix up something important here - what you think @kevinxw ?

Yes, you will have to put it via text editor. GUI will set empty value to default value, which is ‘on’.

Let me know if it works