The code does not work. The script never gets called But I got the lights on automation to work, I added the initial_state: on to it.
the binary_sensor was sensor in the code so that is why it did not work. Now the script is called but the light won’t turn off. There is a huge error in the logs:
Error while executing automation automation.bathroom_lights_off_when_no_motion. Unknown error for call_service at pos 1:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 382, in action
await script_obj.async_run(variables, context)
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 131, in async_run
await self._handle_action(action, variables, context)
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 210, in _handle_action
action, variables, context)
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 299, in _async_call_service
context=context
File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 85, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context)
File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1121, in async_call
self._execute_service(handler, service_call))
File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1143, in _execute_service
await handler.func(service_call)
File "/usr/local/lib/python3.6/site-packages/homeassistant/components/script.py", line 94, in turn_off_service
in component.async_extract_from_service(service)], loop=hass.loop)
File "/usr/local/lib/python3.6/asyncio/tasks.py", line 304, in wait
raise ValueError('Set of coroutines/Futures is empty.')
ValueError: Set of coroutines/Futures is empty.
Here is the code:
Automation
- alias: 'Bathroom lights off when no motion'
id: 'Bathroom lights off when no motion'
initial_state: 'on'
trigger:
- platform: state
entity_id: sensor.bathroom_motion_sensor
to: 'on'
- platform: state
entity_id: binary_sensor.bathroom_motion_sensor_timing
to: 'off'
action:
- service: script.turn_off
entity_id: script.switch_off_lights_delay
- condition: template
value_template: >
{{ is_state('sensor.bathroom_motion_sensor', 'off') }}
- service: script.switch_off_lights_delay
Script:
switch_off_lights:
alias: Switch off lights
sequence:
- delay:
minutes: >
{{ '15' if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else '2' }}
- service: light.turn_off
entity_id: light.bathroom
Binary_sensor
binary_sensor:
- platform: template
sensors:
bathroom_motion_sensor_timing:
friendly_name: 'Bathroom motion sensor timing'
value_template: >
{{ is_state('sensor.bathroom_motion_sensor','on')}}
delay_off:
minutes: 1
device_class: motion