Value_template evaluating now() does not trigger

Hi

I want to turn on/off a switch based on an input_boolean state and the current day.
Use-Case is to select the days on which a switch has to be turned on/off in the UI.
The switch activates/deactivates my lawn mower robot.

Two automation rules are checking if now() equals the selected day and activate the switch.
The check (trigger) looks like:

((now().strftime('%a')) == 'Sun' and is_state('input_boolean.mow_sun', 'on')) or ((now().strftime('%a')) == 'Mon' and is_state('input_boolean.mow_mon', 'on'))  ...

I was expecting that a trigger involving now().strftime('%a') fires on each day transition, which is not the case. :frowning:
I suppose it maybe only gets evaluated once on startup?

Any ideas on how to solve this?
I know I could check with a time trigger for input state changes but I suppose this is unnecessary stress for the system?

Thanks


Here the relevant snippets:

input_boolean:
  mow_mon:
    name: Monday
    initial: on
    icon: mdi:calendar
  mow_tue:
    name: Tuesday
    initial: on
    icon: mdi:calendar
    ...
automation:
 - id: '10'
    alias: 'Mower ON'
    hide_entity: false
    trigger:
      platform: template
      value_template: "{{ ((now().strftime('%a')) == 'Sun' and is_state('input_boolean.mow_sun', 'on')) or ((now().strftime('%a')) == 'Mon' and is_state('input_boolean.mow_mon', 'on')) or ((now().strftime('%a')) == 'Tue' and is_state('input_boolean.mow_tue', 'on')) or ((now().strftime('%a')) == 'Wed' and is_state('input_boolean.mow_wed', 'on')) or ((now().strftime('%a')) == 'Thu' and is_state('input_boolean.mow_thu', 'on')) or ((now().strftime('%a')) == 'Fri' and is_state('input_boolean.mow_fri', 'on')) or ((now().strftime('%a')) == 'Sat' and is_state('input_boolean.mow_sat', 'on')) }}"
    condition:
      condition: state
      entity_id: switch.husquarna_automower_310_switch
      state: 'off'
    action:
      - service: switch.turn_on
        entity_id: switch.husquarna_automower_310_switch
      - service: notify.pushetta
        data:
          message: 'turned mower ON'

  - id: '11'
    alias: 'Mower OFF'
    hide_entity: false
    trigger:
      platform: template
      value_template: "{{ ((now().strftime('%a')) == 'Sun' and is_state('input_boolean.mow_sun', 'off')) or ((now().strftime('%a')) == 'Mon' and is_state('input_boolean.mow_mon', 'off')) or ((now().strftime('%a')) == 'Tue' and is_state('input_boolean.mow_tue', 'off')) or ((now().strftime('%a')) == 'Wed' and is_state('input_boolean.mow_wed', 'off')) or ((now().strftime('%a')) == 'Thu' and is_state('input_boolean.mow_thu', 'off')) or ((now().strftime('%a')) == 'Fri' and is_state('input_boolean.mow_fri', 'off')) or ((now().strftime('%a')) == 'Sat' and is_state('input_boolean.mow_sat', 'off')) }}"
    condition:
      condition: state
      entity_id: switch.husquarna_automower_310_switch
      state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.husquarna_automower_310_switch
      - service: notify.pushetta
        data:
          message: 'turned mower OFF'

Ok, after thinking a bit more intense about this, I might have resolved it by myself :wink:

Addad a template sensor:

  - platform: template
    sensors:
      day_today:
        friendly_name: 'Today'
        value_template: '{{ (now().strftime("%a")) }}'

And check for the sensor state:

value_template: "{{ ((states.sensor.day_today.state) == 'Sun' and is_state('input_boolean.mow_sun', 'on')) or ((states.sensor.day_today.state) == 'Mon' and is_state('input_boolean.mow_mon', 'on')) or ...

Will see tomorrow if it worked…

Edit: works now :relaxed:

1 Like