Good evening,
I’m trying to tidy up my automations a bit by using templates, but i’ve hit a brick wall with this one. I’ve got some motion sensors to control the lights and depending on the time of day, they switch on different scenes. The time of day also controls how long the lights will stay on for after the motion sensor reports no motion. The different times of day are stored in an input_select list and are called “Day”, “Evening”, “Night” and “Movie”.
Before i started using templates i just had separate automations that were triggered by motion and had the input_select value as the condition, but this looked untidy and i wanted to combine them all into a single automation. I was able to use a template for the action part of the automation which selects the light scene based on the input_select value, but i can’t figure out how to get templating to work with the “for” option in the trigger section to control how long the lights stay on for after the motion sensor returns a false. The time that i’d like the lights to stay on for is stored in a input_number variable.
I found the following example on the github pull requests which added templating to the “for” option back in Jul 19 (Pull request 24912):
automation:
- alias: Use template in trigger's for option
trigger:
platform: state
entity_id: sensor.a
to: some_state
for:
minutes: "{{ states('input_number.minutes')|int }}"
seconds: "{{ states('input_number.seconds')|int }}"
action:
Based on this i came up with the following trigger template:
id: '1650815020918'
alias: Living light auto off debug
description: ''
trigger:
- platform: template
value_template: '''{{ is_state(''input_boolean.living_light_request'', ''off'') }}'''
for:
minutes:
data_template: |-
{% if is_state('input_select.light_mode_flat','Day') %}
{{ states('input_number.motion_living_not_detected_day') | int }}
{% elif is_state('input_select.light_mode_flat','Evening') %}
{{ states('input_number.motion_living_not_detected_evening') | int }}
{% elif is_state('input_select.light_mode_flat','Night') %}
{{ states('input_number.motion_living_not_detected_night') | int }}
{% elif is_state('input_select.light_mode_flat','Movie') %}
{{ states('input_number.motion_living_not_detected_evening') | int }}
{% endif %}
condition: []
action:
- service: scene.turn_on
target:
entity_id: scene.living_all_lights_off
metadata: {}
mode: single
This obviously doesn’t work and i’m sure there’s a simple reason why, but i just can’t see it.
Any help to get to the bottom of this would be greatly appreciated.
Cheers
David
Hassio running natively on a RPi 4
Version : core-2022.4.6
Version : supervisor-2022.04.0
Operating System : Home Assistant OS 7.6
1 post - 1 participant
Read full topic