Hi, basically I want the light in my kitchen to come on when someone enters the room. I have two scenes, Welcome and Night. I want the scene that comes on to be determined by the time of day that someone enters the room. How would I do this? I’m assuming something like this
# Turn on Kitchen Light if motion is detected
- id: kitchen_motion_on
alias: Turn on kitchen light when there is motion in the kitchen
trigger:
platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor_2_0
to: "on"
action:
- service: homeassistant.turn_on
data_template:
entity_id: >
{% if TIME > 23:00 %}
scene.welcome
{% else %}
scene.night
I would rather not make two separate automations for this because I may want to do this for even more scenes/time conditions
What would I put in the TIME area? Thanks.
So this is what I have put together, but its thowing an error
# Turn on Kitchen Light if motion is detected
- id: kitchen_motion_on
alias: Turn on kitchen light when there is motion in the kitchen
trigger:
platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor_2_0
to: "on"
action:
- service: homeassistant.turn_on
data_template:
entity_id: >
{% if states.sensor.time.state > 6:00 && states.sensor.time.state < 23:00 %}
scene.welcome
{% else %}
scene.night
{% endif %}
The error is
2017-06-12 16:45:47 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'integer') for dictionary value @ data['action'][0]['data_template']['entity_id']. Got None. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/
I think that your operands are not correct, the way that I read this is greater than 6AM but before 23:00(PM) so that’s why your night time would trigger. But I could be wrong
What I’m going for is past 6am and before 11pm turn on the welcome scene, other than that turn on the night scene. Can you think of a better way to write it?
Tip with avoiding this kind of stuff - use the templates dev tool (icon at bottom of the left-side bar in HA UI). Even if you don’t get a helpful value, per se, you can at least avoid syntax errors.
How is your time sensor defined?
It looks like you are trying to do a string compare to “6:00” and that’s an entirely different thing than a time compare.
Here is a template compare that uses current time.
{%if (now().time().strftime("%H")| int > 6 ) and(now().time().strftime("%H")| int < 23 )%}
scene.welcome
{% else %}
scene.night
{% endif %}
I will thanks. HADashboard is based on Appdaemon now right?
I think I have figured it out though, just had to switch some things around
# Turn on Kitchen Light if motion is detected
- id: kitchen_motion_on
alias: Turn on kitchen light when there is motion in the kitchen
trigger:
platform: state
entity_id: binary_sensor.kitchen_multi_sensor_sensor_2_0
to: "on"
action:
- service: homeassistant.turn_on
data_template:
entity_id: >
{% if (states.sensor.time.state > "23:00") and (states.sensor.time.state < "6:00") %}
scene.night
{% else %}
scene.welcome
{% endif %}
seems to be working now, just need to test tonight after 11 to be sure.