Conditional actions - is there an if/then/else construct?

Hi,

I have an automation that turns on a light when a motion sensor detects motion. One of the classics…
Anyway, I’d like to use different settings for the light, depending on if the light has been turned off less then 5 minutes before.
In that case I’d like to restore the last state of the light and otherwise turn it on with maximum brightness.

I know I could do something like:

action:
  - condition: state
    entity: group.bedroom_lights
    state: 'off'
    for:
      minutes: 5
 - service: homeassistant.turn_on
   entity_id: script.bedroom_timed_lamp

But how do I add an action that is executed if the condition is not matching?
Or can I do something like

action:
  - condition: template
    value_template: '{{ false }}'
  - service: foo
  - condition: template:
    value_template: '{{ true }}'
  - service: bar

So if the first condition evaluates to false and the second condition to true, the action after the second condition is executed?
I’m not really sure about the scope of these conditions. Do they only apply to the next following action or until the end of the action:-block?

Sebastian

1 Like

I think you can use templates to do different actions depending of a sensor.

You can check

Thanks, that looks promising! I’ll give it a shot.

Sebastian

Basically, I would go that way

1 - Define a binary sensor that changes to ‘on’ with an automation when the light has been off for 5 minutes
2 - Define an other automation to make that sensor off as soon as the light is turned back on

Then your automation for the brightness should be somthing like (not sure though can not test it now)

service: light.turn_on
data_template:
  entity_id: light.YourLIght
  {% if is_state('binary_sensor.YOURBINARYSENSOR', 'off') %}
      brightness: 255
  {% endif %}

(not sure you can put the brightness: in the template like that tough). A workaround might be to define two scripts, one to simply turn_on light, the second to turn on and set brightness. Then do

service: script.turn_on
data_template:
  entity_id: >-
  {% if is_state('binary_sensor.YOURBINARYSENSOR', 'off') %}
      script.script2      
  {% elif is_state('binary_sensor.YOURBINARYSENSOR', 'on') %}
      script.script1
  {% endif %}

I tried that before, but it seems you can’t use templates with light.turn_on.
Just for testing, I wrote it like this:

- service: light.turn_on
  data_template:
    entity_id: light.bedroom
    {% if is_state('light.bedroom', 'off') %}
    brightness: 254
    {% endif %}

That fails the configtest with

ERROR (SyncWorker_1) [homeassistant.util.yaml] while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/scripts/bedroom_timed_lamp.yaml”, line 9, column 10
ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/scripts/bedroom_timed_lamp.yaml”, line 9, column 10

Sebastian

no the problem is from using that form of a template juste after entity_id without specifing the attribute.

The script version I wrote above should work though, as the template is after entity_id:

Hm, I still get the same “found character ‘%’ that cannot start any token” error.
Just to test the syntax, I did this:

action:
  - condition: state
    entity_id: group.bedroom_lights
    state: 'off'
  - service: script.turn_on
    data_template:
      entity_id: >-
      {% if is_state('group.bedroom_lights', 'off') %}
        script.bedroom_timed_lamp
      {% else %}
        script.bedroom_timed_lamp2
      {% endif %}

Sebastian

indent problem maybe ?

action:
  - condition: state
    entity_id: group.bedroom_lights
    state: 'off'
  - service: script.turn_on
    data_template:
      entity_id: >-
        {% if is_state('group.bedroom_lights', 'off') %}
          script.bedroom_timed_lamp
        {% else %}
          script.bedroom_timed_lamp2
        {% endif %}
1 Like

You’re right! The template below entity_id needs to be indented one more level.
It looks like it’s working now :slight_smile:

Sebastian

You could also use a service template:

Thanks, I didn’t know that you could template the service that’s called.
But this wouldn’t help much, I guess, since I know I want to turn on the light.
So the template needs to be in the service attributes (e.g. entity_id).

It looks like this is doing what I want:

action:
# when turning on the light less then 5 minutes after it was turned off
# restore the last brightness state instead of setting it to max brightness
  - condition: state
    entity_id: group.bedroom_lights
    state: 'off'
  - service: script.turn_on
    data_template:
      entity_id: >-
        {% if relative_time(states.group.bedroom_lights.last_changed) > "5 Minutes" %}
        script.bedroom_timed_lamp
        {% else %}
        script.bedroom_timed_lamp_restore
        {% endif %}

BTW: love this relative_time() function! - Whoever wrote this is a genius :wink:

EDIT: Unfortunately arithmetics with relative_time() doesn’t work as I would’ve liked…
I had to replace

    {% if relative_time(states.group.bedroom_lights.last_changed) > "5 Minutes" %}

with

    {% if as_timestamp(utcnow()) - as_timestamp(states.group.bedroom_lights.last_changed) > 5*60 %}

Not quite as elegant, but it works :slight_smile:

Sebastian

5 Likes

FYI in case someone else comes to this thread for another example, I came up with this working to conditionally send notifications to various places.

https://github.com/superm1/homeassistant-config/commit/13db9ad7179d1dfc7d373e7abc04f876c1df7af9

1 Like

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!