Automation: different actions based on condition

Good day everyone!

For now I have 6 different automations for bathroom light, they are working fine. Now I want to cover one more scenario. Here is automation which I want to make better:

- id: '1538321511928'
  alias: Bath off on door motion and light
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'on'
  - condition: state
    entity_id: switch.wall_switch_right_158d000245c5ca
    state: 'on'
  action:
  - delay: 0:0:1
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off

I want to use humidity sensor and turn off the light not in 1 second if there is hi humidity but in a couple of minutes. I do not want to make two automations, it seems to me that templates can help here.
Like if humidity is hi - then delay 180 sec
else delay 1 sec

Should I look for template in ‘action’ or in condition? In documentation i found only notification examples, so it did not help me.

Any advices?

1 Like

Service_template is what you’re looking for :+1:

1 Like

I do something like this for my automations and scripts:

- service_template: >
      {% if is_state('binary_sensor.neil_home', 'on') %}
        script.startup_neil_home
      {% else %}
        script.startup_neil_not_home
      {% endif %}
1 Like

You can use a template directly in the delay step. Not sure what sensor you’re using for humidity, but something like this should work for you:

  - delay: "{{ 1 if states('sensor.HUMIDITY')|float < HI_HUMIDITY else 180 }}"

Thank you, it worked. But if I leave it with 3 minutes and then again open the door (trigger that automation again) lights go off immediately
As I understand, if i trigger the automation second time - delay is just skipped, so I can not use it in this situation. Is there another option for delay?

Trying to get ‘delay’ in script to avoid issue described earlier
Seems like I need something like this

  - service_template: >
        {% if states.sensor.humidity_158d0002279c7d | float > 60.0 %}
          script.hi_humidity
        {% else %}
          script.low_humidity
        {% endif %}

But service_template is wrong to use here as I have to write entity_id and just choose which service to use as is described in https://www.home-assistant.io/docs/scripts/service-calls/

Tried this

  action:
  - entity_id: "{{ script.1540143367881 if states('sensor.humidity_158d00022833b3')|float > 60 else script.1542453568731 }}"
    service: script.turn_on

but home assistant did not like that:

Invalid config for [automation]: Entity ID {{ script.1540143367881 if states('sensor.humidity_158d00022833b3')|float &gt; 60 else script.1542453568731 }} is an invalid entity id for dictionary value @ data['action'][0]['entity_id']. Got None. (See /config/configuration.yaml, line 118). Please check the docs at https://home-assistant.io/components/automation/

action:
  service_template: >
    {% if states('sensor.humidity_158d00022833b3')|float > 60 %} script.1540143367881
    {% else %} script.1542453568731 {% endif %} 

In general you want to avoid the situation where an automation might trigger while the actions are still running from a previous trigger. The usual way I handle that is to move the actions into a script. Then, in the automation action you first cancel the script (in case it’s still running from a previous invocation), and then start it. So, automation:

- id: '1538321511928'
  alias: Bath off on door motion and light
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'on'
  - condition: state
    entity_id: switch.wall_switch_right_158d000245c5ca
    state: 'on'
  action:
    - service: script.turn_off
      entity_id: script.turn_off_light
    - service: script.turn_off_light

And script:

turn_off_light:
  sequence:
  - delay: "{{ 1 if states('sensor.humidity_158d0002279c7d')|float < 60 else 180 }}"
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off
1 Like

Hey man, a lot of things I learn from you. Thank you again, I did not think about stopping and then triggering the script. Tried it out - worked just how it should.

1 Like

And one more thing. If I would like to turn off the light after humidity goes lower than 60%, then I sould use wait_template?

'1556739575323':
  alias: Bath off
  sequence:
  - wait_template: "{{ states.sensor.humidity_158d0002279c7d|float < 60 % }}"
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off

Home assistant does not think that I can use wait_template in script

Yes, but you have a couple of errors in the template as you wrote it.

You either need to use states('sensor.humidity_158d0002279c7d'), which is preferred, or states.sensor.humidity_158d0002279c7d.states. The way you wrote it you were trying to convert an entire State Object to float, which would not work. Also, you have a stray % character. Lastly, you might want to include a timeout. So:

'1556739575323':
  alias: Bath off
  sequence:
  - wait_template: "{{ states('sensor.humidity_158d0002279c7d')|float < 60 }}"
    timeout: '00:30:00'
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off

Note that in recent HA versions the default for a wait_template with a timeout is to continue the script if the timeout is exceeded. (It used to abort if that happened.)

1 Like

Sorry for annoying. I still can not figure out how to prevent scenario when script is on (waiting for lower humidity) and then I go to bath again, then humidity gets lower then 60% and lights turn off while I am there :frowning:
I would use condition (if there is no motion) but then script does not work when humidity is ok and lights should be turned off in 1 second.
Maybe I could count how long script is on and if more than 2 minutes (xiaomi morion sensor is on for 2 minutes after actual motion) then check the motion sensor. If less than two minutes - wait until 2 minutes are gone and then check motion sensor.
Say if I want too much from this automation. Unfortunately, I can not use recent one because my father or mother would be really confused going bathroom after someone.

I’m a little confused by what you want, and what all the sensors do. How about this? Do you want the light to stay on as long as:

  1. humidity is above 60%, or
  2. light has not been on for at least two minutes after automation is triggered, or
  3. motion sensor is indicating motion

If so, then maybe add a timer to your config:

timer:
  bathroom_light:
    duration: '00:02:00'

Then your script would be:

'1556739575323':
  alias: Bath off
  sequence:
  - service: timer.start
    entity_id: timer.bathroom_light
  - wait_template: >
      {{ states('sensor.humidity_158d0002279c7d')|float < 60 and
         is_state('timer.bathroom_light', 'idle') and
         is_state('binary_sensor.motion_sensor_158d000224aa83', 'off') }}
    timeout: '00:30:00'
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off

This would wait until the humidity is less than 60%, and it’s been at least two minutes since the automation was triggered, and the motion sensor has stopped indicating motion. But if that doesn’t happen within 30 minutes of the automation being triggered, then the light would be turned off anyway.

Sorry, I was not clear
Firstly - our bathroom light is responsible for working fan too (I have no idea how to translate it, but it makes bathroom less wet. So main point - if humidity is hi - I need the lights to be on)
Secondly, I don’t want lights to turn off while someone is in bathroom and humidity gets from 65 to 55%.

With this code HA always will wait at least two minutes after me going away from bathroom. Here is what I would like to achieve:
I open the door (door sensor)
If humidity is less than 60% - turn light off in 1 second
If humidity is higher than 60% - wait for humidity sensor to get less than 60, BUT I do not want to come back in 5 minutes, when humidity is 65% and then be confused when lights get off. I just can not yet come up with solution.

Maybe I can use door sensor to abort the script, which is waiting for humidity to get less than 60%? So If I will go to bathroom while script is waiting, I will break it and light will not turn off while I am in bathroom even if humidity will get less than 60%

If not - that is fine. I will think couple of days on the algorithm.

And sorry for being not really clear - English is not my native language.

Thanks, that explanation helps.

I think what you want is:

  1. If door is opened (i.e., you’re leaving the bathroom), and the humidity is below 60%, then turn off the light and connected fan in one second.
  2. If the humidity goes from 60% or above to below 60%, and the door is open (i.e., nobody is in the bathroom), then turn off the light and connected fan.

Do you think that would work for you? (Note, I don’t see where the motion sensor even has to be involved.)

If so, then:

- alias: Turn off light/fan when leaving non-humid bathroom
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.humidity_158d0002279c7d
    below: 60
  action:
    - delay: 1
    - service: switch.turn_off
      entity_id: switch.wall_switch_right_158d000245c5ca

- alias: Turn off light/fan when humidity drops and nobody in bathroom
  trigger:
    platform: numeric_state
    entity_id: sensor.humidity_158d0002279c7d
    below: 60
  condition:
    condition: state
    entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.wall_switch_right_158d000245c5ca

Will try out this one tonight
And motion sensor is in bathroom

Update.
I looked closer to the code. Actually, we does not leave bathroom with open door, so that condition will not work.

Tried this. Seems to work

Automations:

- id: '1538321511928'
  alias: Bath off on door motion and light
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.motion_sensor_158d000224aa83
    state: 'on'
  - condition: state
    entity_id: switch.wall_switch_right_158d000245c5ca
    state: 'on'
  - condition: state
    entity_id: script.1556739575323
    state: 'off'
  action:
  - entity_id: script.1556739575323
    service: script.turn_off
  - service: script.1556739575323

- id: '1540301700624'
  alias: Bath pretend to turn off
  trigger:
  - entity_id: binary_sensor.door_window_sensor_158d0001e5d691
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: script.1556739575323
    state: 'on'
  action:
  - data:
      entity_id: script.1556739575323
    service: script.turn_off

script:

'1556739575323':
  alias: Bath wait for humidity then off
  sequence:
  - delay: 0:0:1
  - wait_template: "{{ states('sensor.humidity_158d0002279c7d')|float < 60 }}"
    timeout: '00:30:00'
  - data:
      entity_id: switch.wall_switch_right_158d000245c5ca
    service: switch.turn_off
I have a few cameras running on a Blue Iris Server that feeds Home Assistant via a MQTT feed for the motion sensors.
3 Fire Tablets throughout the house running Fully Kiosk Browser on Fire Tablets.
Currently I have automation and scripts setup to go to full screen of that camera for 3 minutes on each of the Fire Tablets and then return to the base home assistant start page after 3 minutes. This is a static automation without conditions.
Please if I could receive assistance on:
Motion --> Full screen video pops up on Fully Kiosk Browser, need assistance with conditional statement/timer that keeps that full screen up if there is continued motion triggered (Currently it is set after 3 minutes to return back to the home screen regardless of future events)
Motion --> Full screen video pops up on Fully Kiosk, need assistance with a conditional statement that if there is any other motion triggered within 3 minutes of each other to bring up a different video url that includes all the cameras: http://192.168.87.66:81/mjpg/index/video.mjpeg. In addition, if there is persisted motion on any of the cameras to extend the video on the tablets rather than returning to the home screen.
Example: Front Door Motion, Full Screen Front Door Camera Video URL comes on Fire Tablets via Fully, 1 minute passes, Motion at Driveway --> I want at this point for a different URL to be sent to Fully Kiosk Browser that includes all the cameras (listed above).
Lastly, I have 2 of the Fire Tablets that go dim/screen off after no motion for 30 seconds or so. display.turn_on via Fully does turn on the screens. I’d like to be able to integrate display.turn_on throughout the video sequences above and not have them go dark.
Example of Current Automations/Scripts:
Front Door Motion Triggered – Scripts to pull up video on Kiosk Tablets
- alias: FD Motion
  trigger:
    platform: state
    entity_id: binary_sensor.mqtt_front_door_motion
    to: 'on'
  action:
  - service: homeassistant.turn_on
    entity_id: script.fdimage
  - service: homeassistant.turn_on
    entity_id: script.motion_fd_alexa
  - service: homeassistant.turn_on
    entity_id: script.fdmotionlr
  - service: homeassistant.turn_on
    entity_id: script.fdmotionbr
  - service: homeassistant.turn_on
    entity_id: script.fdmotionmini
  id: 86d5f5ca35c94f40a2f97e281e42e0d0
Script Example:
fdmotionlr:
  sequence:
    - service: display.turn_on
      entity_id: display.living_room_tablet
    - delay:
        seconds: .3    
    - service: fully_kiosk.screensaver_stop
      entity_id: display.living_room_tablet
    - delay:
        seconds: .3
    - service: display.load_url
      entity_id: display.living_room_tablet
      data:
        url: http://192.168.87.66:81/mjpg/FD/video.mjpeg
    - delay:
        minutes: 3
    - service: display.load_url        
      entity_id: display.living_room_tablet
      data:
        url: http://192.168.87.68:8123/lovelace/Tablet
Any help is very much appreciated, thank you!