I would like to implement and expand one of the most classic automations ever: activating the lights through a motion sensor.
I am using Danielbook’s blueprint as a basis for this. To increase the acceptance of HA automations with my girlfriend, only one lamp in the hallway (flur) should be turned on at night and all three lamps during the day.
To achieve this, I have extended Daniel’s blueprint with an additional “light input” (light_target_night) and declared variables for the inputs. At night, “light_target_night” should now be used instead of “light_target” for the “light.turn_off” and “light.turn_on” services.
I thought I could achieve this most easily by implementing the selection of the service target through an if…else condition:
Unfortunately, this implementation leads to the following error message when the automation runs (trace timeline):
Error in describing action: Cannot use ‘in’ operator to search for ‘area_id’ in {% if today_at(states(local_night_end)) < now() or now() < today_at(states(local_night_start)) %} {{ local_light }} {% else %} {{ local_light_night }} {% endif %}
All lights in the area or Zigbee group (entity) are always turned on, regardless of the time. I am surprised by this because the else branch does not refer to the Zigbee group/area, but to the single light entity.
My automation is set up as follows:
Thank you very much for the hint! It makes sense! I’ll test it.
So I guess in Daniels blueprint this works because the direct !input of the target was used instead of a variable so the “context” (area/group/entity) gets delivered with it and gets lost when writing the !input into a variable?
What if I don’t use the variables and use the direct !input again?
Okay I tried this. There is a diffrent error now: Error: Template rendered invalid entity IDs: {'entity_id': 'light.flur_decke'}
‘light.flur_decke’ is the Zigbee group entity.
So instead of using templating to select the entity_id / device_id / area_id to be used by the service I had to re-write the whole action-part of the blueprint:
action:
- if:
- condition: or
conditions:
- condition: template
#evaluates to true if now() is between helper entity "Start of Day/End of night" and "End of Day/Begin of night"
value_template: >-
{{ today_at(states(local_night_end)) < now() and now() <
today_at(states(local_night_start)) }}
#this happens during the day
then:
- service: light.turn_on
target: !input light_target
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: 'on'
- delay:
seconds: "{{ local_motion_delay_day }} - {{ local_motion_dim_time }}"
- service: light.turn_off
data: {transition: !input dim_time}
target: !input light_target
#this happens during the night
else:
- service: light.turn_on
target: !input light_target_night
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: 'on'
- delay:
seconds: "{{ local_motion_delay_night }} - {{ local_motion_dim_time}}"
- service: light.turn_off
data: {transition: !input dim_time}
target: !input light_target_night
The link to the blueprint gist can still be useful if someone has a similar problem to solve.