Help with automation - Dynamic Switch Scheduler

Hello there,

I’m a newbie in Home Assistant but I’m trying to create a proof of concept that is a bit too advanced for me but it is something I would find pretty cool if it worked :slight_smile:

I want to create a way to schedule switches that my girlfriend could use without any programming (in this example, start my dishwasher during the night). I created a Slider to set the time, a Boolean to turn on the Schedule and an IFTTT trigger just to notify my phone so that I know it is working.

Here is my code:

automation:
  trigger:
    platform: time
    minutes: '/3'
    seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'input_boolean.dishwasher_auto_switch'
        state: on
      - condition: template
        value_template: '{{ states.input_boolean.dishwasher_start_time.state = now.hour }}'
  action:
    service: ifttt.trigger
    data: {"event":"DISHWASHER_START", "value1":"Dishwasher has started"}

When I boot Home Assistant I get the following error:

16-10-03 00:11:12 homeassistant.bootstrap: Error during setup of component automation
Traceback (most recent call last):
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/bootstrap.py", line 102, in _setup_component
    result = component.setup(hass, config)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 168, in setup
    success = _process_config(hass, config, component)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 299, in _process_config
    cond_func = _process_if(hass, config, config_block)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 365, in _process_if
    checks.append(condition.from_config(if_config))
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/helpers/condition.py", line 37, in from_config
    return factory(config, config_validation)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/helpers/condition.py", line 43, in and_from_config
    config = cv.AND_CONDITION_SCHEMA(config)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 192, in __call__
    return self._compiled([], data)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 486, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 324, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data['conditions'][0]['condition']

Does anyone know what’s wrong?

Thanks in advance!

Unquote the entity_id and quote the state:

      - condition: state
        entity_id: input_boolean.dishwasher_auto_switch
        state: 'on'

Thank you for your reply. I did the changes you mentioned and the error still occurs. Here’s the current version of the code:

automation:
  trigger:
    platform: time
    minutes: '/3'
    seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.dishwasher_auto_switch
        state: 'on'
      - condition: template
        value_template: '{{ states.input_boolean.dishwasher_start_time.state = now.hour }}'
  action:
    service: ifttt.trigger
    data: {"event":"DISHWASHER_START", "value1":"Dishwasher has started"}

What am I doing wrong?

Thanks in advance!

You should have a different error now though; can you post it?

Since version 0.29 now is a function, so replace now.hour with now().hour.

EDIT: not that that really matters, because how could the state of an input_boolean ever be equal to now().hour? An input_boolean is either ‘on’ or ‘off’.

Oh thank you. It was an input_slider. That error message didn’t really help :s ill try this when I get home.

Where can I find the list of functions I can use in templates?

Thanks.

Have a look at this bit of documentation. That page doesn’t show the changes to now and utcnow yet, I just submitted a Pull Request to have that fixed.

Also this should help:

I managed to change the error. I think I corrected the first condition by adding quotes to entity_id and state. Here’s the current code:

automation:
  trigger:
    platform: time
    minutes: '/3'
    seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'input_boolean.dishwasher_auto_switch'
        state: 'on'
      - condition: template
        value_template: '{{ states.input_slider.dishwasher_start_time.state = now().hour }}'
  action:
service: ifttt.trigger
data: {"event":"DISHWASHER_START", "value1":"Dishwasher has started"}

Here’s the current error:

16-10-04 10:09:10 homeassistant.bootstrap: Error during setup of component automation
Traceback (most recent call last):
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/bootstrap.py", line 102, in _setup_component
    result = component.setup(hass, config)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 168, in setup
    success = _process_config(hass, config, component)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 299, in _process_config
    cond_func = _process_if(hass, config, config_block)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/components/automation/__init__.py", line 365, in _process_if
    checks.append(condition.from_config(if_config))
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/helpers/condition.py", line 37, in from_config
    return factory(config, config_validation)
  File "/volume1/@appstore/py3k/usr/local/bin/homeassistant/helpers/condition.py", line 43, in and_from_config
    config = cv.AND_CONDITION_SCHEMA(config)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 192, in __call__
    return self._compiled([], data)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 486, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages/voluptuous/schema_builder.py", line 324, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: not a valid value for dictionary value @ data['conditions'][1]['condition']

Thanks in advance.

To check if two values are equal you need to use == instead of =

And for the record: I’ve never seen the need to quote an entity_id, ever. Just look around the docs, I’d be very surprised if you find an example anywhere.

Thanks! It’s working now. I also found out the template screen where you can test templates which makes everything way easier. Had to convert the slider value to int for the comparison to work. Here’s the final version:

automation:
  trigger:
    platform: time
    minutes: '/3'
    seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: 'input_boolean.dishwasher_auto_switch'
        state: 'on'
      - condition: template
        value_template: '{{ states.input_slider.dishwasher_start_time.state|int == now().hour }}'
  action:
    service: ifttt.trigger
    data: {"event":"DISHWASHER_START", "value1":"Dishwasher has started"}

The example with the quotes I found was here:
https://home-assistant.io/getting-started/scripts-conditions/

1 Like

Might the issue mentioned in this thread be the reason that my automation doesn’t work either?
Can you point out what I need to change to get it working again?

- alias: 'Turn lights off Garden'
  trigger:
    platform: template
    value_template: '{{ now.hour == 23 and now.minute == (states.input_slider.random_minute.state | int) }}'
  action:
    - service: light.turn_off
      data: 
        entity_id: light.hue_white_1_tuin

As stated above, since version 0.29 of Home Assistant now is a function. So replace now.hour with now().hour and now.minute with now().minute.

The rest seems correct.

Still doesn’t work :frowning:

Have you tested your value template in the template editor under the developer tools in the HASS UI?

Yes. But I’m not sure how to test it in combination with the second part.
It keep saying false:

- alias: Get Random Time
  trigger:
    platform: time
    after: '22:00:00'
  action:
    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.hour
        value: '{{ (range(22, 23) | random) }}'
    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.random_minute
        value: '{{ (range(37, 39) | random) }}'

- alias: 'Turn lights off Garden'
  trigger:
    platform: template
    value_template: '{{ now().hour == 23 and now().minute == (states.input_slider.random_minute.state | int) }}'
  action:
    - service: light.turn_off
      data: 
        entity_id: light.hue_white_1_tuin

Result now that it’s exactly 23:38:

- alias: Get Random Time
  trigger:
    platform: time
    after: '22:00:00'
  action:
    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.hour
        value: '22'
    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.random_minute
        value: '38'

- alias: 'Turn lights off Garden'
  trigger:
    platform: template
    value_template: 'False'
  action:
    - service: light.turn_off
      data: 
        entity_id: light.hue_white_1_tuin

anyone?
Currently my automatic lights off automation doesn’t work like I want.

Can anyone please help me?

I can’t get this to work:

  trigger:
    platform: template
    value_template: '{{ now().hour == 21 and now().minute == (states.input_slider.random_minute.state | int) }}'

Random minute state is filled like this:

    - service: input_slider.select_value
      data_template:
        entity_id: input_slider.random_minute
        value: '{{ (range(30, 45) | random) }}'

It worked in the past but not since recent versions. Do I need to change “int” to something else?
Value is currently: 43.0

This is how I was able to get this working:

Enable sensor.time:

sensor:
  - platform: time_date
    display_options:
      - time

And the automations look like this:

automation:
  - alias: set_random_minute
    trigger:
      - platform: time
        after: '22:00:00'
    action:
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.random_minute
          value: '{{ (range(0, 60) | random) }}'

  - alias: turn_light_off_garden
    trigger:
      - platform: template
        value_template: "{{ is_state('sensor.time', '21:' ~ states('input_slider.random_minute')|round(0)) }}"
    action:
      - service: light.turn_off
        data: 
          entity_id: light.hue_white_1_tuin