Set different entity ID in automation action, based on time of day

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.

You could add the time platform and use that entity ID.

Thanks, I’ll give that a try, do you know how in jinja I could test if the time is between 23:00 and 6:00?
I’m thinking something like this

states.sensor.time.state < 23:00 and states.sensor.time.state > 6:00

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/

Suggestions?

Sorry, Im horrible with Jinja and templates, Im sure someone will be along soon to help.

I’ve fixed the error, had to add quotes around the times and replace the && with and

          {% if states.sensor.time.state > "6:00" and states.sensor.time.state < "23:00" %}
            scene.welcome
          {% else %}
            scene.night
          {% endif %}

Now it appears that It’s doing the opposite of what it is suppose to because its only 10 am where I am, but the night scene is being activated.

1 Like

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.

This seems better suited for AppDaemon. You seem knowledgeable enough where it might be your cup of tea.

Yea, I plan on getting into AppDaemon, just haven’t gotten around to playing with it yet.

Do it now. :slight_smile: This is an easy win for appdaemon as it’s a good use and not overly complicated for an appdaemon beginner.

Sorry, I read it too quickly and confused your scenes. From what I see, this should work (logically).

Msg me in gitter and I’ll help you if you need it.

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.

Yes. Install HADashboard. Its the newer version of appdaemon. (which is very confusing)