Change switch on duration through slider

Hi!

I was customizing the above code for a different switch where instead of the preset time triggers, the switch on time is set by a helper(H1) and the on hours(H2) is also set by a helper

For the last part of the code, to switch off the device, for the datetime variable what would be the correct syntax so it the switch off time is on time (H1)+on hours(H2)
I tried >{{states(‘input_datetime.5x5_lights_on_time’)}}, but it yields an error

Message malformed: template value should be a string for dictionary value @ data[‘action’][1]['data

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.5x5_light_off_time
data:
  datetime: >-
    {{ {{states('input_datetime.5x5_lights_on_time')}} + timedelta(hours=states('input_number.5x5_light_on_hours')|int(1))
    }}

That depends. Does your datetime have a time and date, or just a time?

Try this in the template editor:

data:
  datetime: "{{ today_at(states('input_datetime.5x5_lights_on_time')) + timedelta(hours=states('input_number.5x5_light_on_hours')|int(1)) }}"

Thank you, this works perfectly, btw, I just have the time since the switches go through the routine daily.

The helper (off time) does not update the value till the automation is run, which I get,however is there anyway where I can create a time helper that has it’s value conditional upon a different helper?

You could create a template sensor (instead of a helper) that would update when any of the entities in the template updated, e.g.

template:
  - sensor:
      - name: "5x5 Lights Off Time"
        icon: "mdi:clock"
        device_class: timestamp
        state: "{{ today_at(states('input_datetime.5x5_lights_on_time')) + timedelta(hours=states('input_number.5x5_light_on_hours')|int(1)) }}"

This will update if you change the datetime or number helpers.

You use it in the same way:

trigger:
  - platform: time
    at: sensor.5x5_lights_off_time
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_

And remove the off time setting service from the on automation as you no longer need it (it updates automatically):

alias: Watering
description: ''
trigger:
  - platform: time
    at: '07:30:00'
  - platform: time
    at: '12:30:00'
  - platform: time
    at: '17:30:00'
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.sonoff_

The only issue here is that you cant edit the off time directly if you ever need to. Say you wanted to cut the time short for some reason, you’d have to change the on_hours input number or on time.

I tried doing this but can’t create a sensor in the sensor.yaml file

If I create a new sensor.yaml and include it in the configuration.yaml,this is the error it shows

Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 10).

I posted the the sensor directly in the configuration.yaml and it works.

Can I use the switch off code in the switch on automation? Just so it reduces the no of automations I have.

It’ll look like this

alias: Watering
description: ''
trigger:
  - platform: time
    at: '07:30:00'
  - platform: time
    at: '12:30:00'
  - platform: time
    at: '17:30:00'
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.sonoff_

trigger:
  - platform: time
    at: sensor.5x5_lights_off_time
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_

The code I posted goes in configuration.yaml not sensors.yaml.

The sensor uses the new template integration, not the legacy template sensor platform.

1 Like