Automation data_template now() with minutes

I have an automation which sets my ceiling light to moonlight between 10pm and 7am when they are turned on.

- alias: Deckenlampe Diele Normal/Mondlicht
  trigger:
    platform: state
    entity_id: light.deckenleuchte_diele
    to: on
  action:
    service: light.yeelight_set_mode
    data_template:
      entity_id: light.deckenleuchte_diele
      mode : >
        {% if now().hour >= 22 %}
        moonlight
        {% elifif now().hour <= 7 %}
        moonlight
        {% else %}
        normal
        {% endif %}

But I would like to do this after 10:30pm and before 07:30pm. Anyone has an idea?

Thanks!

      mode: >
        {% set n = now() %}
        {% if n.hour > 22 or
              n.hour == 22 and n.minute >= 30 or
              n.hour < 7 or
              n.hour == 7 and n.minute < 30 %}
          moonlight
        {% else %}
          normal
        {% endif %}
2 Likes