Automation based on time stored as sensor state

Hello.

I’m trying to carry out an automation in which the trigger is before a time stored in the state of a sensor and one of the conditions is also after a time stored in the state of a sensor.

I have 2 sensors like this:
sensor.time_from 20:30:00 sensor.time_to 08:30:00

And my automation:
- alias: 'Tomada Diogo' trigger: platform: time after: '{{ strptime(states.sensor.time_to, "%T") }}' condition: condition: and conditions: - condition: time before: '{{ strptime(states.sensor.time_from, "%T") }}' - condition: state entity_id: 'switch.tomada_diogo' state: 'on' action: - service: switch.turn_off entity_id: 'switch.tomada_diogo'

Hass always starts with errors in automation:
ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: Invalid time specified: {{ strptime(states.sensor.time_to, "%T") }} for dictionary value @ data['trigger'][0]['after']. Got None extra keys not allowed @ data['condition'][0]['conditions'][0]['before']. Got None not a valid value for dictionary value @ data['condition'][0]['conditions'][0]['condition']. Got None required key not provided @ data['condition'][0]['conditions'][0]['entity_id']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 65). Please check the docs at https://home-assistant.io/components/automation/

Can anyone help me achieve this automation, please?

Thanks in advance for any insights.

Please use preformatted text when posting YAML.

“after/before” does not accept a template. Use a template condition for that kind of thing.

Would this make more sense (at least HASS doesn’t complain about it anymore)?

- alias: 'Tomada Diogo'
  trigger:
    platform: state
    entity_id: 'switch.tomada_diogo'
    state: 'on'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ strptime(states.sensor.time_to, "%T") < strptime(now(), "%T") }}'
      - condition: template
        value_template: '{{ strptime(states.sensor.time_from, "%T") > strptime(now(), "%T") }}'
  action:
    - service: switch.turn_off
      entity_id: 'switch.tomada_diogo'

Thanks again for your help.

Just some advice - you can check those value templates in the template dev tool. If you plug in the template, you’ll want to see “true” or “false”.