Change switch on duration through slider

Hi!

I have sonoff switches that are scheduled to switch on at fixed times (lovelace)

I would like to edit the time they stay on for through an input slider.

I tried making a helper and then converting it into a sensor which can be used with lovelace, but they allow only to read the states currently.

Any other way?

Put the helper (input_number) your dashboard.

Yes!

I have the slider on the dashboard.

I cannot figure out how to switch off the switch after " input slider minutes"

I created the below automation that switches the pump on at preset times and switches it off after 3 minutes, can you check it and let me know what should I put at the delay value so it takes the slider value

alias: Watering
description: ''
trigger:
  - platform: time
    at: '07:30:00'
  - platform: time
    at: '12:30:00'
  - platform: time
    at: '17:30:00'
condition: []
action:
  - type: turn_on
    device_id: 
    entity_id: switch.sonoff_
    domain: switch
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 
    entity_id: 
    domain: switch
mode: single

So why did you say this instead?

It helps if you ask your actual question. We are not mind readers.

This will turn your light off after the time (in minutes) specified in your input_number:

trigger:
  - platform: state
    entity_id: switch.sonoff_
    to: 'on'
    for:
      minutes: "{{ states('input_number.your_slider_here')|int(1) }}"
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_

Keep this as your turn on automation:

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_

Note: If you restart home assistant or reload automations (which happens when you save a new automation in the UI editor) it will interrupt/cancel the off timer automation. If that is likely to be an issue use these automations instead:

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_
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.watering_off_time
    data:
      time: "{{ (now() + timedelta(minutes=states('input_number.your_slider_here')|int(1))).strftime("%H:%M:%S") }}"
trigger:
  - platform: time
    at: input_datetime.watering_off_time
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_

You have to create the helper: input_datetime.watering_off_time

EDIT: this has been substantially corrected as below.

Hi!

I am so sorry if I was not clear.

I am able to get a slider on the dashboard by creating a helper for input number, I don’t know how to utilize it in the automation.

Thank you for your tip regarding the reloading automation part, I will be following your method!

So, I have to create one helper for input_number and another one for input_datetime, right?

The above code calls the switch to turn on and then the date time service?
Is the entity_id: input_datetime.XXX, correct? I did not understand the .XXX part

And where do I paste the below code?

trigger:
  - platform: time
    at: input_datetime.watering_off_time
condition: []
action:
  - service: switch.turn_off
    target:
      entity_id: switch.sonoff_

Use input_datetime.watering_off_time instead of input_datetime.XXX. That was a mistake I have corrected above.

In another automation.

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_
  - service: input_datetime.watering_off_time
    target:
      entity_id: input_datetime.watering_off_time
    data:
      time: "{{ now() + timedelta(minutes=states('input_number.your_slider_here')|int(1) }}"

Hi!

Pasting this in the YAML editor gives the below error

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

Google says its some punctuation error mostly, could you look at it once again?

Did you change input_number.your_slider_here to your slider entity id?

Did you create input_datetime.watering_off_time ?

It could also be this issue with the editor:

Oh shucks!

yes I did change input_number.your_slider_here and I did create input_datetime.watering_off_time.

I am editing in the automation tab itself, I’ll give it a try in the studio visual editor.

Will that make the automation work?

Anyway I can introduce input_datetime.watering_off_time as a service and then add the automation through the UI editor?

I don’t understand what you mean by this. It is an entity. Not a service.

You can use YAML mode in the UI editor.

Oh!

It was called as a service in the code, so I figured it had to be configured as one.

I am using the YAML mode in the UI Editor :woozy_face:, that is what gave me the error

Tried pasting the code as you’ve mentioned in the automations.yaml file, the 2nd automation does not get called and the switch does not turn off

Missed a bracket:

    data:
      time: "{{ now() + timedelta(minutes=states('input_number.your_slider_here')|int(1)) }}"

Still not working!

I am pasting the code for both the automations below

 id: "1657002760640"
  alias: 4x4 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_1000eca7dd
      data: {}
    - service: input_datetime.watering_off_time
      target:
        entity_id: input_datetime.watering_off_time
      data:
        time: "{{ now() + timedelta(minutes=states('input_number.4x4_watering_time')|int(1)) }}"
- id: "1657003150380"
  alias: 4x4_Watering_Minutes_Slider
  description: ""
  trigger:
    - platform: time
      at: input_datetime.watering_off_time
  condition: []
  action:
    - service: switch.turn_off
      target:
        entity_id: switch.sonoff_1000eca7dd
  mode: single

Also, can this work?

After calling the switch.on service for the entity, I add an action for delay and entered the below code, where I substituted the minutes for the state of the slider, it gives me the float error

Error: expected float for dictionary value @ data[‘minutes’]

What am I missing to convert it into a float?

- delay:
      hours: 0
      minutes: {{ states('input_number.4x4_watering_time') | float(0)}}
      seconds: 0
      milliseconds: 0
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sonoff_1000eca7dd

Look at the automation traces to see why.

Also check what input_datetime.watering_off_time set to after the on automation fires.

This is the error that I am getting now

Unable to find service input_datetime.watering_off_time

The value for the input_datetime.watering_off_time does not change after switching on the automation, I have not set it to any value, it just shows 12:00 AM.

the 2nd step that calls for the switch to be off, does correctly calculate the time according to the slider though

params:
  domain: input_datetime
  service: watering_off_time
  service_data:
    time: '2022-07-05 14:36:19.492455+05:30'
  target:
    entity_id:
      - input_datetime.watering_off_time
running_script: false
limit: 10

Ok not sure what drugs I was on when I wrote that. It should be:

  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.watering_off_time
    data:
      time: "{{ now() + timedelta(minutes=states('input_number.your_slider_here')|int(1)) }}"

Haha!

Still not working

You did not change it.

 - service: input_datetime.set_datetime

Ahh! Sorry!

Now it says this

Invalid time specified: 2022-07-05 15:20:42.882263+05:30 for dictionary value @ data['time']

So I changed time to datetime and it works!!!

time: "{{ now() + timedelta(minutes=states('input_number.your_slider_here')|int(1)) }}"

to

datetime: "{{ now() + timedelta(minutes=states('input_number.your_slider_here')|int(1)) }}"

Thank you so much for your help!!

Ok the other option would have been to format the time:

  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.watering_off_time
    data:
      time: "{{ (now() + timedelta(minutes=states('input_number.your_slider_here')|int(1))).strftime("%H:%M:%S") }}"

I’ll update the original automations in case anyone else see it.

1 Like