Template to Choose Entity by Day

Hey All:

Having an issue with a template; maybe someone sees an obvious problem with this code:

- id: weeklychoresreset
  alias: 'Weekly Chores Reset'
  trigger:
  - platform: time
    at: '00:01:00'
  action:
  - service: input_boolean.turn_off
    data_template:
      entity_id: >
        {% set day = (as_timestamp(now())|timestamp_custom ('%a')) %}
        {% if day == 'Sun' %} 'input_boolean.chore_mop_floors'
        {% elif day == 'Mon' %} 'input_boolean.chore_vacuum_bedrooms'
        {% elif day == 'Tue' %} 'input_boolean.chore_clean_kids_bathroom'
        {% elif day == 'Wed' %} 'input_boolean.chore_vacuum_upstairs'
        {% elif day == 'Thu' %} 'input_boolean.chore_clean_ensuite'
        {% elif day == 'Fri' %} 'input_boolean.chore_vacuum_downstairs'
        {% elif day == 'Sat' %} 'input_boolean.chore_sweep', 'input_boolean.chore_clean_down_bathroom'
        {% else %} 'none'
        {% endif %}

As you can see, the point is for the HA to check what day of the week it is, and turn off one of a number of different boolean switches depending on the day. For example, on Sunday only input_boolean.chore_mop_floors should turn off. Saturday should be both input_boolean_chore_sweep and input_boolean.chore_clean_down_bathroom.

I’m here because this isn’t working (e.g. this morning, input_boolean.chore_clean_kids_bathroom was still on when it should have turned off). Can you guys and gals see anything obviously wrong? This resolves correctly in the template developer tools (i.e. today, Tuesday, it resolves to ‘input_boolean.chore_clean_kids_bathroom’) but it doesn’t seem to be correctly passing the entity_id to the turn_off service.

Thanks.

Did you try without single quotes around the entity ids?

I… think that worked. Do I not need the quotes around the ones for Saturday, or is the comma enough?

Thanks for your help.

Hmm not sure on this one, just try it with just the comma. If this doesn’t work it probably works when you put them into a list. Just come back if it doesn’t work :slightly_smiling_face:

I’ll play around with it and figure out what works. I just asked in case you knew the answer off the top of your head. Thanks a ton for catching that stupid mistake.

I plan on putting this into the “share” forum once I get it finished. It’s actually vaguely elegant, considering the source of the strings (me).

That wasn’t a "stupid’ mistake, I looked at the documentation and other posts and came back with nothing definitive and could only suggest what @Burningstone said.
Don’t kick yourself over this one, up to today, if I’d been pushed, I’d have written it just like you did.
I’ve learnt a wrinkle today (I suspect burning has too) so join the club.
All in all, you wrote a template which was pretty damn close to a solution.
Drink a beer to celebrate :beers:

Edit: can you please post your working template, just to help the next guy ?

1 Like

I’m definitely at the end of the spectrum where I don’t kick myself - I’m more amused when it actually works if I’ve created it with only minimal copy and paste. Hobbyist coder - it’s still fun for me, even when the mistakes can be rectified with a quick delete. That’s my definition of a stupid mistake - not a, “Well, let’s try to recreate that into a function that might actually work” but instead a, “Oh, yeah you’re really not clear when to use ’ and when to use " (or, when to use neither in this case) in YAML are you?”

Anyway, enough about me, you came here for the recipe (lol). This seems to be working:

- id: weeklychoresreset
  alias: 'Weekly Chores Reset'
  trigger:
  - platform: time
    at: '00:01:00'
  action:
  - service: input_boolean.turn_off
    data_template:
      entity_id: >
        {% set day = (as_timestamp(now())|timestamp_custom ('%a')) %}
        {% if day == 'Sun' %} input_boolean.chore_mop_floors
        {% elif day == 'Mon' %} input_boolean.chore_vacuum_bedrooms
        {% elif day == 'Tue' %} input_boolean.chore_clean_kids_bathroom
        {% elif day == 'Wed' %} input_boolean.chore_vacuum_upstairs
        {% elif day == 'Thu' %} input_boolean.chore_clean_ensuite
        {% elif day == 'Fri' %} input_boolean.chore_vacuum_downstairs
        {% elif day == 'Sat' %} 
          input_boolean.chore_sweep, input_boolean.chore_clean_down_bathroom
        {% else %} 'none'
        {% endif %}
1 Like

As you can tell from the above “not always” :rofl:
Hopefully, the above example will dig someone else out of the brown stuff.
Everyday is a learning experience, part of the reason I help out is so I can learn.
You are doing well and have this under your belt.
Happy coding