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)

This is a bit more complicated than I expected. It will not be a simple flag to tell the automation to always respect the illuminance sensor. If I do this, then the moment the light is turned on, illuminance sensor will return a value above the threshold, then the light will be off immediately. You will see the light flashing in this case. I need to think about some sort of timeout in this case, which means more conditions to consider.

Hello,

I’m quite new to HA and really appreciate the blueprints, what a great idea to be able to share automations! I’m hoping you might be able to help me as I try to figure out where I’m going wrong with the automation.

What I’d like to do is turn on a light switch when a contact sensor is open and then turn the switch off when the sensor is closed. If I’m understanding the automation correctly this should be able to be done inside of the single automation and I shouldn’t need to create a second “off” automation.

Share my yaml file that was created from the automation

alias: Late Night Snack Lights
description: ''
use_blueprint:
  path: kevinxw/state_based_entity_control.yaml
  input:
    trigger_entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    trigger_entity_state: 'On'
    target_entity: light.living_room_lamp
    trigger_timeout: '00:05:00'
    target_entity_state: none
    target_entity_action_on: homeassistant.turn_on
    target_entity_action_off: homeassistant.turn_off
    trigger_entity_state_off: 'Off'
    target_entity_off: light.living_room_lamp

Appreciate any help/guidance you might be able to offer, thank you!

Hi James,

I believe you only need the following


alias: Late Night Snack Lights
description: ''
use_blueprint:
  path: kevinxw/state_based_entity_control.yaml
  input:
    trigger_entity: binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off
    target_entity: light.living_room_lamp
    trigger_timeout: '00:05:00'

This will turn off your light 5 minutes after your door is closed. Let me know if it works.

1 Like

Thanks for the advise, Kevin. I think what I was looking for was a way to turn off the light when the sensor is closed rather than a timeout, would that still be possible with this blueprint?

Yes. Just put 0 to the timeout.

Hi - I’m facing issues using entities in the automation but direct values work.

I’m trying to use ‘Illuminance Below’ with input_number however the automation fails conditions. If I replace the entity with a value it works.

I’m getting a similar error using an input_number for trigger timeout. The automation complains it’s not in the correct format, e.g. ‘HH:MM’ but it is. If I use ‘10’ in the automation it works, but ‘00:10’ in the input_number complains:

Error: offset input_text.motion_attic_timer should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F'

Are input_number’s supported in the automation or do I need to use values? Was hoping to create a dashboard to easily change the values.

Thanks for any help - awesome blueprint by the way

IMG_7678
Hi kevinxw
I have 2 Motion Sensor for the Balcony’s door and lamp.
I set
1.Motion Sensor2 for cat.When Sensor2 detected a motion the door will open 10%.
2.Motion Sensor1 for human.When Motion Sensor1&2 detected a motion the door will open 60% and turn on light.

But now when Sensor2 or Sensor1 detected a motion the door alway open 10%.How to except Automation 1 when Automation 2 action?

Hi,
I am not sure why Illuminance Below doesn’t work. Do you see any error when you use the input_number?

For trigger timeout, it should be input_text as the error states. It’s not an input number.

I think your requirement is not related to this blueprint. But you can create such an automation from scratch:

  1. Create a blank automation;
  2. Put both sensor1 and sensor2 as two separate entity triggers, leave the entity state as blank;
  3. Put the rest of the logic in your action using “choose”. You will have something like this:
alias: Door Automation
description: ''
mode: restart
trigger:
  - platform: state
    entity_id: sensor1
  - platform: state
    entity_id: sensor2
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor1
            state: 'on'
        sequence:
          - service: open_door_60%
      - conditions:
          - condition: state
            entity_id: sensor2
            state: 'on'
        sequence:
          - service: open_door_10%
    default:
      - service: close_door

Thanks.now everything is OK.
alias: Door Open
description: ‘’
trigger:

  • platform: state
    entity_id: binary_sensor.cat_motion_sensor
  • platform: state
    entity_id: binary_sensor.human_body_sensor
    condition: []
    action:
  • choose:
    • conditions:
      • condition: state
        entity_id: binary_sensor.human_body_sensor
        state: ‘on’
        sequence:
      • device_id: 33ce731e57f49561f3e0bbd24492faa5
        domain: cover
        entity_id: cover.yyunyi_yypy24_5e71_window_opener
        type: set_position
        position: 50
    • conditions:
      • condition: state
        entity_id: binary_sensor.cat_motion_sensor
        state: ‘on’
        sequence:
      • device_id: 33ce731e57f49561f3e0bbd24492faa5
        domain: cover
        entity_id: cover.yyunyi_yypy24_5e71_window_opener
        type: set_position
        position: 11
        default: []
        mode: restart``

Any update on this?
(related to 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) - #54 by e-raser and 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) - #57 by e-raser)

I wanted to use this automation to do the most basic thing it was designed for: turning a target entity on if trigger entity goes on and turning target entity off as soon as trigger entity switches to off. On, off. In this case: turn vent on when window is open, turn vent off once the window is closed.

BUT: it doesn’t work. Makes me crazy because it’s SO SIMPLE.

Here’s my automation:

use_blueprint:
  path: kevinxw/state_based_entity_control.yaml
  input:
    trigger_entity: binary_sensor.contact_window
    target_entity: switch.plug_stove_vent

So everything else is default. Where´s the fault? :man_shrugging:

Trace logs always the same:
grafik

I ended up creating my own simple automation for now but I want to sort this one out because - this just should work right.

For this, I have decided to not change the blueprint. I coded half way and found it will be too hard to maintain all the states in one blueprint given this additional condition. So I will not make change this time. Sorry for the wait.

For this use case, I recommend you use my other blueprint: https://github.com/kevinxw/homeassistant/blob/55fa2a2867292dd44f7b631daf5d476345019c6e/blueprints/sync_two_entities_states.yaml

All it does is to syncronize the states of two entities.

The original blueprint in this post is not perfect fit for this job. It introduces a timeout after the state change.

I’ve noticed that this automation doesn’t seem to work (executes the ‘default’ action) if the sun elevation is blank. If I set it to 95 or any other value, the automation runs as expected. Took me awhile to figure this out, not sure why I’m effected by this.

Hmm, not very sure if anything changed in homeassistant recently. I have a few automations left the elevation blank and they work. By default the blueprint will fill the value with 91 if blank.

Interesting… I have Adaptive Lighting installed and I am not seeing issues with this blueprint reacting to light level changes for my Hue light strip in the kitchen.

That said, there is the mystery of why it came on several times in the middle of the night yesterday (though has been installed for a week or more and that’s first time it has happened). I suspect there was something else going on as we had a thunderstorm, the garage light, also controlled by a motion sensor but using a different blueprint, was acting weird in those couple of hours, too.

I also am looking for a way to control lights based on presence but some of the time having presence control when to turn the light on for a period of time (limited by luminescence, solar elevation and/or time) OR having presence control when to turn the light off (i.e., when light is turned on by anything other than this blueprint, wait a period of time without motion before turning off, often measured in minutes or hours). Basically, my goal is to have the light in workshop (plugged into zigbee outlet) come on anytime anyone enters and stay on for a few minutes after motion stops OR to walk into the shop, turn on the light via a zigbee switch and have the light stay based on one of three scenarios (choosable in UI ahead of time):

  1. till light turned off by switch
  2. for n period (20 minutes, 3 hours, etc.)
  3. till midnight

I think I can handle those scenarios ok using the blocking features of this blueprint. What I need though is a way to tell whether the light was turned on by the automation created with this blueprint or though any other means. So far, I have not found a way to do that but it seems like since you provide a way to change how a device is turned on or off, there is surely an answer.

Thoughts?

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.