Problem with condition

I have two automations with conditions where i want two different things to happen depending on day/date.
But seems like the automation doesn’t work at all on fridays…

Any brainiac that could see whats wrong?

  - condition: time
    weekday:
    - fri
  - condition: template
    value_template: '{{ now().strftime("%d") == "13" }}'

And the other

  condition:
  - condition: time
    weekday:
    - fri
  - condition: template
    value_template: '{{ now().strftime("%d") == "1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31" }}'

Is this a script or an automation?

If script, what triggers it, and what’s the full script.

If automation when is it supposed to run, and when is it not supposed to run?

Sorry automations

Full automations

- alias: 'Friday 13'
  initial_state: true
  trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'on'
  condition:
  - condition: time
    weekday:
    - fri
  - condition: template
    value_template: '{{ now().strftime("%d") == "13" }}'
  - condition: state
    entity_id: media_player.all
    state: 'off'
  - condition: state
    entity_id: media_player.kitchen
    state: 'off'
  action:
  - service: script.turn_on
    entity_id: script.friday13

and

- alias: 'Welcome hallway 2'
  initial_state: true
  trigger:
  - platform: state
    entity_id: sensor.hallway_motion_sensor
    to: 'on'
  condition:
  - condition: time
    weekday:
    - fri
  - condition: template
    value_template: '{{ now().strftime("%d") == "1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31" }}'
  - condition: state
    entity_id: input_boolean.hallway_tts
    state: 'off'
  - condition: state
    entity_id: media_player.kitchen
    state: 'off'
  - condition: state
    entity_id: media_player.all
    state: 'off'
  action:
  - service: script.turn_on
    entity_id: 
      - script.hallway

Template for the second one won’t work (which explains why it didn’t trigger today for you), should be

{{ now().strftime("%d") != "13" }}

Other than that I can’t see any problems off the top of my head.

Thanks for trying but that didnt solve it

So for the second one, do you only want it to run on Fridays, but for the 1st one you only want it to run on Friday the 13th? I think you intended for the second automation to run all times not including friday the 13th. If so you could remove

  - condition: time
    weekday:
    - fri

try:

- condition: template
  value_template: "{{ now().weekday() == 4 and now().day == 13 }}"

now().weekday() is based on monday being 0, so friday is 4.

now().day is the day of the month, based on 1 being the 1st.

EDIT: I’m guessing you want friday the 13th, so that should work.

I don’t see how this would ever work either. I think you wanted this:

'{{ now().strftime("%d") in "1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31" }}'

Using == meants that it’s trying to match exact strings.

strftime(“%d”) will return the day number and it will always be between ‘0’ and ‘31’. It will never equal ‘1,2,3…etc’

Using in will see if the string is inside the other.

that will only work on friday the 13th. never any other day.

So I guess my question is… what is your goal here? are you trying to separate friday the 13th from the other fridays? If so, all you need is this (to replace both conditions):

To find friday the 13th:

- condition: template
  value_template: "{{ now().weekday() == 4 and now().day == 13 }}"

to find all other fridays

- condition: template
  value_template: "{{ now().weekday() == 4 and now().day != 13 }}"

would it be possible to have the same for weekdays?

If you want all weekdays:

value_template: "{{ now().weekday() in range(5) }}"

Nevermind, im getting tired :tired_face:

Now, how would i know if your help works :slight_smile:

@petro,
Somewhat related, I took example from above and trying to set conditional after/Before based on weekday or weekend (want light to go on later on weekend).

But below is not currently working. Problem is in Conditional Template.
Works fine in Template editor, but not in reality. Can you guide?

    ####################################################################
    # Motion in Bedroom
    ####################################################################
    - id: Motion in BR  # Normal Weekdays
      alias: 'Motion in BR'
      trigger:
        platform: state
        entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor_2
        to: 'on'
      condition:
        - condition: template
          value_template: >- 
            {% if now().weekday() in (0,1,2,3,4) %}
            after: '07:00:00'
            before: '19:44:00'
            {% else %}
            after: '09:00:00'
            before: '19:44:00'
            {% endif %}
      action:
        - service: homeassistant.turn_on
          entity_id: 
            - switch.mbr__bryan_light

Thanks,
~Bryan

That won’t work. after and before are not attributes of a template condition. Also, you cannot place config sections inside templates at all. Templates only return a single string, meaning it cannot include a key (after:, before:) and it can only return 1 item.

You’ll have to do this long hand. I.E. Caculate the current time in minutes, calculate the after time in minutes, calculate the before time in minutes, and then compare them.

OK. Bummer. Thank you.

This would work.

        - condition: template
          value_template: >- 
            {% set before = 19*60+44 %}
            {% set after = 7*60 if now().weekday() in (0,1,2,3,4) else 9*60 %}
            {% set current = now().hour*60+now().minute+now().second/60 %}
            {{ after <= current <= before }}
1 Like

@petro I bow before the – thank you!