Adding a condition to a generalised automation

I have the following automation which turns on the tablet screen in each room when motion is detected. This works fantastically well except for bedrooms late at night.

I would like to add a condition for the bedrooms so that when motion is detected there is also a check for the bedoorm’s light level:

if sensor.bedroom1_illuminance < 5, turn off screen in bedroom1
if sensor.bedroom2_illuminance < 5, turn off screen in bedroom2
if sensor.bedroom3_illuminance < 5, turn off screen in bedroom3

else, turn on.

All the other rooms can remain on.

Is there an easy way to extend this without needing to get into a spaghetti of branching conditions?

alias: Turn on tablet screens and set URL
description: turn on wallpanel screen when motion detected
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroomguest
      - binary_sensor.bathroommain
      - binary_sensor.bedroom1
      - binary_sensor.bedroom2
      - binary_sensor.bedroom3
      - binary_sensor.hallwaylarge
      - binary_sensor.hallwaysmall
      - binary_sensor.kitchen
      - binary_sensor.lounge
      - binary_sensor.study
    to: "on"
condition: []
action:
  - service: mqtt.publish
    data:
      topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
      payload: "{\"wake\": true}"
mode: single

Add a condition and check if the illuminance is below 5 when the automation was triggered from one of the bedroom sensors.

condition:
  - condition: template
	value_ template: >-
	  {% if trigger.entity_id in ('binary_sensor.bedroom1', 'binary_sensor.bedroom2', 'binary_sensor.bedroom3') %}
		{% set mapIlluminance = {'binary_sensor.bedroom1':'sensor.bedroom1_illuminance',
						 'binary_sensor.bedroom2':'sensor.bedroom2_illuminance',
						 'binary_sensor.bedroom3':'sensor.bedroom3_illuminance'} %}

		{{ states(mapIlluminance.get(trigger.entity_id, 'sensor.bedroom1_illuminance')) | int(0) < 5 }}
	  {% else %}
		True
	  {% endif %}

Thanks for the quick reply @dennis84de. I’m confused by this line and it being specific to bedroom1. Could that be a typo?

Can an automation only ever send one action? For example, to really perfect this automation, I’d like to send a different payload when motion is detected but the room is dark and turn off the tablet.

For example cancelling the wake lock:

action:
  - service: mqtt.publish
    data:
      topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
      payload: "{\"wake\": false}"

The illuminance sensor in this line is only a fallback for the case the entity_id from the trigger is not in “mapIlluminance”.

You could use if / then or choose action in your automation in order to do something different when it is dark:

action:
  - if: >-
		<Check for illuminance>
	then:
	  - service: mqtt.publish
		data:
		  topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
		  payload: "{\"wake\": false}"  
    else:
	  - service: mqtt.publish
		data:
		  topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
		  payload: "{\"wake\": true}"
1 Like

Thanks so much @dennis84de

alias: Turn on tablet screens and set URL
description: turn on wallpanel screen when motion detected
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroomguest
      - binary_sensor.bathroommain
      - binary_sensor.bedroom1
      - binary_sensor.bedroom2
      - binary_sensor.bedroom3
      - binary_sensor.hallwaylarge
      - binary_sensor.hallwaysmall
      - binary_sensor.kitchen
      - binary_sensor.lounge
      - binary_sensor.study
    from: 'off'
    to: 'on'
condition: []
action:
  - variables:
      light_sensor: 'sensor.{{ trigger.to_state.object_id }}_illuminance'
      is_bright: '{{ states(light_sensor) | int(0) >= 5 }}'
  - service: mqtt.publish
    data:
      topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
      payload: "{\"wake\": {{ is_bright }} }"
mode: single
  • If the light sensor’s value is greater than or equal to 5, the value of is_bright is set to true and that’s what is passed to payload.

  • If it’s less than 5, is_bright is false and that’s what’s passed to payload.


EDIT

Correction. light_sensor variable contains entity_id of a sensor entity.


NOTE

If the naming format of each light sensor is simply the name of its associated motion sensor plus “_illuminance” then a map isn’t required and a simple template can do the trick (as demonstrated in the light_sensor variable).

Thanks @123 - I really like this approach.

Thanks for all the help so far. Unfortunately I’m stuck with an error Message malformed: Expected a dictionary @ data['action'][0]['if'][0]in the automation.

I’m also a little more clear about what I’m trying to do now:

  1. if motion detected in bedroom(1,2,3)
    1.1) brightness >5, turn on screens
    1.2) brightness <5 turn off screens
  2. all other rooms, turn on screen (which has the effect of cancelling the web screensaver)

I’ve tried to pull together both approaches (@123 correctly points out that the naming overlap helps (eg binary_sensor.bedroom1 and sensor.bedroom1_illuminance but I can’t see how to handle only bedrooms.

Here’s what I’ve got so far. Would it make sense to break this into two automatons (bedrooms/non-bedrooms)?:

alias: Turn on/off panels
description: When motion detected, and only if brightness >5, turn on turn on wallpanel screen. When motion detected, and brightness <5, turn off panels.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroomguest
      - binary_sensor.bathroommain
      - binary_sensor.bedroom1
      - binary_sensor.bedroom2
      - binary_sensor.bedroom3
      - binary_sensor.hallwaylarge
      - binary_sensor.hallwaysmall
      - binary_sensor.kitchen
      - binary_sensor.lounge
      - binary_sensor.study
    from: "off"
    to: "on"
condition: []
action:
  - if: "{{ (sensor.{{ trigger.to_state.object_id }}_illuminance) | int(0) <= 5 }}"
    then:
      - service: mqtt.publish
        data:
          topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
          payload: '{"wake": false}'
    else:
      - service: mqtt.publish
        data:
        topic_template: wallpanel/{{ trigger.to_state.object_id }}/command
        payload: '{"wake": true, "wakeTime": 180}'
mode: single