Add condition to action in automation

Here’s what I was referring to. Tom_I posted code that you echoed back except in your version value_template now has an extra indent.

You may be using Visual studio Code but are you using a YAML checker with it? I’m asking because your examples, when pasted into my editor, are immediately flagged with YAML errors. Here’s an example (wavy underlining is the checker’s means of identifying potential problems):

Screenshot%20from%202019-05-22%2009-19-29

The problem here is sufficiently evident to not even require a YAML checker. The third line lacks indenting and then the problems mount.

maybe make it a bit simpler too, :

- condition: or
  conditions
    - condition: state
      entity_id: media_player.family_room
      state: 'idle'
    - condition: state
      entity_id: media_player.family_room
      state: 'off'
    - condition: state
      entity_id: media_player.family_room
      state: 'paused'

could be:

- condition: template
  value_template: >
    {{states('media_player.family_room') in ['idle','off','paused']}}

also, for ease of reading, and reported issues with delay, I always use

- delay:
    seconds: 5

the fewer quotes the better :wink:

If you read back I started with something similar but for some reason the template condition caused an error.

used this:

- condition: template
  value_template: >
    {{states('media_player.family_room') in ['idle','off','paused']}}

and is spot on, very nice :smile:

sorry Tom, must have missed the, I only saw the ‘or’ conditions.

can anyone explain the

person: >
      {{ trigger.to_state.name }}
      {%- for s in states.input_boolean
            |rejectattr('entity_id','eq',trigger.entity_id)
            |selectattr('state','eq','on')
            if '_present' in s.entity_id and
               (now() - s.last_changed).total_seconds() < 2 -%}
        {{ (' and ' if loop.last else ', ') ~ s.name }}
      {%- endfor %}

to me, I really don’t understand why some things are there. Take the triggers, in which the name always contains ‘_present’.Why then test for that again in this template?

also for 2 triggers this seems a bit over the top, when either of the triggers could have simply been replaced with the name and entered in the message as variable?

to be honest I copied it from someone else’s automation to announce the name of a person arriving home - by replacing the PERSON with the new arrivals entrance (the name pulled from the input_boolean)…it works so not really looked too deep into the structure.

Maybe 2 triggers as it kicks in for multiple people arriving.

haha, ok cool. been there…

if you ever want to try something else, here’s what I do, and its faultless for many registered ‘people’

    action:
      - service: script.intercom_presence
        data_template:
          entityid: '{{trigger.entity_id}}'
          tostate: '{{trigger.to_state.state}}'
          fromstate: '{{trigger.from_state.state}}'
      - service_template: >
          script.{% if is_state('input_boolean.notify_announcement', 'on') %}announce_presence
                 {% elif is_state('input_boolean.notify_presence', 'on') %}notify_presence
                 {% else %}dummy
                 {% endif %}
        data_template:
          trigger: Presence tracking
          entityid: '{{trigger.entity_id}}'
          tostate: '{{trigger.to_state.state}}'
          fromstate: '{{trigger.from_state.state}}'

and use scripts:

# have the iOS device play sounds
  announce_presence:
    alias: 'Announce presence'
    sequence:
      - condition: template
        value_template: >
          {{tostate in ['home','not_home'] and
            fromstate in ['home','not_home'] }}
      - service: notify.ios_telefoon
        data_template:
          title: >
            {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }}  - {{ 'Home' if tostate == 'home' else 'Away'}} speech
          message: >
            {{-as_timestamp(now()) | timestamp_custom('%X') }} :
              {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} {{ ' arrived home' if is_state(entityid, 'home') else ' left home' }}.
          data:
            push:
              sound: >
                {% if entityid in 
                    ['group.d1','group.d2','group.d3','group.d4'] %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Daughter-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Front-Door-Closed.wav
                  {% endif %}
                {% elif entityid in ['group.wife', 'device_tracker.life360_wife'] %}
                  {% if is_state(entityid, 'home') %}US-EN-Morgan-Freeman-Wife-Is-Arriving.wav
                  {% else %}US-EN-Morgan-Freeman-Good-Night.wav
                  {% endif %}
                {% else%}
                  {% if is_state(entityid, 'home') %}US-EN-Alexa-Husband-Is-Arriving.wav
                  {% else %}US-EN-Alexa-Good-Night.wav
                  {% endif %}
                {% endif %}

# have a message sent to your instance
  notify_presence:
    alias: 'Notify presence'
    sequence:
      - service: notify.notify
        data_template:
          title: >
            {{trigger}}: {{ states[entityid.split('.')[0]][entityid.split('.')[1]].name }} notification
          message: >
            {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
            {{as_timestamp(now()) | timestamp_custom('%X') }} :
            {% if tostate == 'not_home' %}
              {{-name }} left {{fromstate}}
            {% elif fromstate == 'not_home' %}
              {{-name }} arrived at {{tostate}}
            {% else %}
              {{-name }} left {{fromstate}} and arrived at {{tostate}}
            {% endif %}

# have the announcement played over the intercom
  intercom_presence:
    alias: 'Intercom presence'
    sequence:
      - condition: template
        value_template: >
          {{ is_state('input_boolean.announce_intercom', 'on') }}
      - service: media_player.volume_set
        data_template:
          entity_id: >
            {{states('sensor.intercom')}}
          volume_level: >
            {{  states('input_number.intercom_volume')|float }}
#            # {{ states('sensor.intercom_volume')|float  }}
      - service: tts.google_say
        data_template:
          language: >
            {{states('input_select.intercom_language')|lower}}
          entity_id: >
            {{states('sensor.intercom')}}
          message: >
            {% set name = states[entityid.split('.')[0]][entityid.split('.')[1]].name %}
              Hi, at {{as_timestamp(now()) | timestamp_custom('%X') }} :
            {% if tostate == 'not_home' %}
              {{-name }} left {{fromstate}}
            {% elif fromstate == 'not_home' %}
              {{-name }} arrived at {{tostate}}
            {% else %}
              {{-name }} left {{fromstate}} and arrived at {{tostate}}
            {% endif %}
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

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!