Pass time from input_select to time trigger

I’m trying to pass the time from an input_select to an automation to turn a light on and off. Can’t seems to get any to work. Tried a binary sensor, but it doesn’t get updated, unless I change the time from the input_select.

tank_light_on:
value_template: >-
{% if states.input_select.tank_light_on.state <= now().strftime("%H:%M:%S") and states.input_select.tank_light_off.state >= now().strftime("%H:%M:%S") %}
on
{% else %}
off
{% endif %}
friendly_name: ‘Tank Light On’

Whats the best way to do this?

Hi, i just did smth similar yesterday… took me some hours to get it to work.

I ended up having a “input_text” which i feed my desired time into, and compare it with the sensor time_date. (Both have the same format in my setup)

  trigger:
    platform: template
    value_template: '{{ states.input_text.alarm.state == states.sensor.time__date.state }}'

Be warned - template triggers are evaluated at least once per second. Using them can cause performance problems.

Instead, create a template sensor, and use that in the automation trigger.

1 Like

Fixed using my original binary sensor. Had to change the from using now() to using a sensor that held the time. I think the binary sensor needs something in the value template that will trigger a state change so the binary sensor knows to update, now() doesn’t trigger this.

can you share the whole code please, i’m also looking for a solution to have timed switches set on or off for a specific time

Configuration.yaml


sensor:
  - platform: time_date
    display_options:
      - 'time'  

binary_sensor:    
  - platform: template
    sensors:
      scrubber_light_off:
        value_template: '{{states.input_select.scrubber_light_off.state <= states.sensor.time.state and states.input_select.scrubber_light_on.state >= states.sensor.time.state}}'
        friendly_name: 'Scrubber Light Off'  
        
input_select:
  scrubber_light_on:
    name: Scrubber Light On
    options:
      - "00:00:00"
      - "01:00:00"
      - "02:00:00"
      - "03:00:00"
      - "04:00:00"
      - "05:00:00"
      - "06:00:00"
      - "07:00:00"
      - "08:00:00"
      - "09:00:00"
      - "10:00:00"
      - "11:00:00"
      - "12:00:00"
      - "13:00:00"
      - "14:00:00"
      - "15:00:00"
      - "16:00:00"
      - "17:00:00"
      - "18:00:00"
      - "19:00:00"
      - "20:00:00"
      - "21:00:00"
      - "22:00:00"
      - "23:00:00"
  scrubber_light_off:
    name: Scrubber Light Off
    options:
      - "00:00:00"
      - "01:00:00"
      - "02:00:00"
      - "03:00:00"
      - "04:00:00"
      - "05:00:00"
      - "06:00:00"
      - "07:00:00"
      - "08:00:00"
      - "09:00:00"
      - "10:00:00"
      - "11:00:00"
      - "12:00:00"
      - "13:00:00"
      - "14:00:00"
      - "15:00:00"
      - "16:00:00"
      - "17:00:00"
      - "18:00:00"
      - "19:00:00"
      - "20:00:00"
      - "21:00:00"
      - "22:00:00"
      - "23:00:00" 

Automation.yaml

- alias: Action - Action - Turn On Scrubber Light
  trigger:
    - platform: state
      entity_id: binary_sensor.scrubber_light_off
      to: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.algae_light   
      
- alias: Action - Action - Turn Off Scrubber Light
  trigger:
    - platform: state
      entity_id: binary_sensor.scrubber_light_off
      to: 'on'
  action:
    - service: switch.turn_off
      entity_id: switch.algae_light
1 Like

thank you @Dinoaus

why not through an input slider, looks nicer than a long dropdown list?
not sure if this possible and how?

I guess I could, as I’m interest in only the hour. Could use a slider with values from 0 to 23.

Or maybe a input_text would be easier to read.

Hi there. Today I tried your automation to control the “light times” for my little fish tank. (THANKS for your idea).
At first I thought it isn’t working… I discovered a “problem” with the initial state of the binary sensor during my tests. I set a bunch of times and had to restart hass.io for every test. So, starting up hass.io the binary sensor always comes up with “off” state, and doesn’t turn on the switch because automation is not recognising the “change”. I needed a little time to understand that. Do you have an idea for that little thing, or am I “using it wrong…”

Cheers,

Dave