Motion sensor template action to turn on different scenes

Hi!

I’m brand new to creating automations as I mostly want to “be in control”. But I really wanted to automate the bathroom lamp to turn on at different settings depending on the time of day. While this was possible by creating simple timed rules in different automations I quickly got snugged up in trying to create one automation to rule them all. Addictions and such.

I’ve been trying to learn how to control templates but I seem to have hit a dead end of sorts. The “check configuration” button clears this one for errors which was my first goal. Now I just need to get it to actually launch the scene as well.

- id: bathroom_on
  alias: Badrumssensor på
  trigger:
  - entity_id: binary_sensor.tradfri_motion_sensor
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.yeelight_color2_04cf8c7b5fe9
    state: ‘off’
  action:
    service: scene.turn_on
    data_template:
      entity_id: >
        {%-if now().weekday() in (0,1,2,3,4) and now().hour >=05 and now().hour < 09 () %}
        scene.makeup
        {% elif now().weekday() in (0,1,2,3,4) and now().hour >=09 and now().hour < 18 %}
        scene.daytime_bt
        {% elif now().weekday() in (0,1,2,3,4) and now().hour >=18 and now().hour < 05 %}
        scene.evening_bt
        {%-endif %}

Any suggestions?
The “weekday” thing might go in final configuration as I obviously want them to turn on during the weekend as well.

  1. you have the wrong quote type inside your condition. Notice how the trigger to: 'on' section is red and your state: 'off' section’s quotes look wierd? It’s because you copied that condition from a forum post here. Don’t do that unless it’s formatted properly.

  2. You also had some extra () at the end of your if first if statement.

  3. Your third else if is also a little wrong because it crosses zero. Gotta account for that. But the beauty of that is, you can just use an else statement to cover those hours.

  4. You don’t have a fallback on weekends. Something has to be in the entity_id field when this fires otherwise you’ll get errors. So, move the weekday check to conditions.

  5. (optional) Side note, you can simplify your if statements at the bottom by condensing the time check into a single statement.

- id: bathroom_on
  alias: Badrumssensor på
  trigger:
  - entity_id: binary_sensor.tradfri_motion_sensor
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.yeelight_color2_04cf8c7b5fe9
    state: 'off'
  - condition: template
    value_template: "{{ now().weekday() in range(5) }}"
  action:
    service: scene.turn_on
    data_template:
      entity_id: >
        {%-if 5 <= now().hour < 9 %}
          scene.makeup
        {%- elif 9 <= now().hour < 18 %}
          scene.daytime_bt
        {%- else %}
          scene.evening_bt
        {%- endif %}
1 Like

Wow!
Thanks. I’ve been searching through different forum posts to try and learn how to do this, but I didn’t think of the quote types. I’ll remember that!
I’ll check your code and see what it amounts to :slight_smile:

So this is only used during weekdays?
I’m trying to figure out if that’s actually necessary, or if it would be best just to have them working the same way. Would that be possible just by “removing” the weekday-parameter?

- id: bathroom_on
  alias: Badrumssensor på
  trigger:
  - entity_id: binary_sensor.tradfri_motion_sensor
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.yeelight_color2_04cf8c7b5fe9
    state: 'off'
  - condition: template
    value_template: "{{ now().}}"
  action:
    service: scene.turn_on
    data_template:
      entity_id: >
        {%-if 5 <= now().hour < 9 %}
          scene.makeup
        {%- elif 9 <= now().hour < 18 %}
          scene.daytime_bt
        {%- else %}
          scene.evening_bt
        {%- endif %}

Thank you so much for the help!

If you want it 24/7, then just remove the weekday condition

- id: bathroom_on
  alias: Badrumssensor på
  trigger:
  - entity_id: binary_sensor.tradfri_motion_sensor
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: light.yeelight_color2_04cf8c7b5fe9
    state: 'off'
  action:
    service: scene.turn_on
    data_template:
      entity_id: >
        {%-if 5 <= now().hour < 9 %}
          scene.makeup
        {%- elif 9 <= now().hour < 18 %}
          scene.daytime_bt
        {%- else %}
          scene.evening_bt
        {%- endif %}
1 Like

Ahh I see, so that condition was just for the weekdays condition. (I’m really new to templates).
Thanks again! I really appreciate it.¨

Works like a charm so far!

Why you see a state of motion sensor of tradfri.
I can only see the Battery Level on HA?!

How is it implemented?
I use the Conbee USB-stick for Zigbee-pairing. Do you use the Trådfri Gateway?

Yes, the Conbee Stick is not working by me.

Something’s up as the sensor should be registered as a binary.sensor, not just sensor.
Did it work before, or has it always been like this?

Edit: Sorry, missunderstood. So you use the Trådfri Gateway. Then I’m at a loss. Haven’t tried it myself.

Complained to him and sent back then. I spent several hours trying to combine different things and nothing went longer than 5 minutes.

That’s too bad!
I managed to set up the Conbee stick with just a few taps after updating to hass.io (hassOS). IT was a little harder before that but it always worked. Sure you didn’t just get a bad product?
It’s worked like a charm for me since december-17.