If statement between times

Hi!

I am trying to create an IF statement to change brightness of my lights between 23:00-08:00. I have come up with below, but do not get my if statement to successfully work.

What am I doing wrong?

# If there is motion and its dark turn on the hall lights
#
- action:
  - alias: Lights on
      data_template: 
  entity_id: group.hall 
  brightness: >-
    {% if (states.sensor.time.state > "23:00") and (states.sensor.time.state < "8:00") %}
      255
    {% else %}
      20
    {% endif %}
service: light.turn_on
alias: 'Hall: If there is motion and its dark turn on the lights'
id: '1510180396101'
condition:
- condition: state
  entity_id: group.hall
  state: 'off'
trigger:
  platform: state
  entity_id: binary_sensor.motion_sensor_158d0001d87910
  to: 'on'

You can test templates in the dev tools-> templates, you may have trouble when 23:59 ticks over to 00:00

{% if (states.sensor.time.state > '00:00') and (states.sensor.time.state < '08:00') %}
  255
{% else %}
  20
{% endif %}

This works but you need another condition for 23:00 to 00:00

so…

{% if ((states.sensor.time.state > '23:00') and (states.sensor.time.state < '00:00')) or ((states.sensor.time.state > '00:00') and (states.sensor.time.state < '08:00')) %}
  255
{% else %}
  20
{% endif %}
1 Like

Thank you so much for this! Worked as a treat!

Im trying to follow this to apply to my own automation… what am i missing:

- id: auto_bathroom_on
  alias: Bathroom On
  trigger:
  - entity_id: light.bathroom_light
    platform: state
    to: 'on'
  action:
  - data:
      entity_id: light.bathroom_light
    data_template:
      brightness_pct: >
        {% if ((states.sensor.time.state > '23:00') and (states.sensor.time.state < '00:00')) or ((states.sensor.time.state > '00:00') and (states.sensor.time.state < '07:00')) %}
          100
        {% else %}
          10
        {% endif %}"
    service: light.turn_on
  initial_state: true
  mode: single

I would do it like this…

- alias: Bathroom On
  trigger:
    platform: state
    entity_id: light.bathroom_light
    to: 'on'
  action:
    service: light.turn_on 
    data_template:
      entity_id: light.bathroom_light
      brightness_pct: >
        {% if now().hour in [23, 0, 1, 2, 3, 4, 5, 6] %} 
          100
        {% else %}
          10
        {% endif %}

Although I suspect you actually want the 10 and the 100 the other way round.

Yep i do want them the other way round, ive had them around the other way while i was testing…

Will try this out, i rebooted and now having issues with by ZigBee devices being offline so this will have to wait till i fix that

1 Like