In order not to unnecessarily hammer an external api I often introduce looping scripts with delays.
It seems to me that there is a problem with using ‘Reload Scripts’ if you have scripts that use delay
. Now, I don’t expect the delay itself to be maintained across reloads but I think this goes beyond that.
I have an example below of how I do it followed by the errors I get if I manually start the script after a reload.
I’d be interested if anyone here who has a development instance of HA can reproduce this? Or of course offer an alternative explanation for the errors I get?
script:
smartweather_set_sensor_interval:
sequence:
- service_template: >
script.loop_smartweather_set_sensor_interval
data_template:
interval: >
{% if states('sensor.elevation') | float >= -6 and
states('input_select.sensor_for_darkness') == 'smartweather_average_illuminance' %}
{% if states('sensor.smartweather_average_illuminance') | int <= 5000 %}
00:01:00
{% elif states('sensor.smartweather_average_illuminance') | int <= 7500 %}
00:15:00
{% else %}
00:30:00
{% endif %}
{% elif states('sensor.elevation') | float >= -6 and
states('input_select.sensor_for_darkness') == 'brightness' %}
{% if states('sensor.brightness') | int <= 25 %}
00:01:00
{% elif states('sensor.brightness') | int <= 30 %}
00:15:00
{% else %}
00:30:00
{% endif %}
{% else %}
18:00:00
{% endif %}
#========================================================================
#=== Update the sensor and loop the script that sets the sensor interval
#===
#=== Passed
#=== interval - time to wait between sensor updates
#========================================================================
loop_smartweather_set_sensor_interval:
sequence:
- service: homeassistant.update_entity
entity_id: sensor.smartweather_aaaa
- delay: "00:00:05"
- service: homeassistant.update_entity
entity_id: sensor.smartweather_bbbb
- delay: "00:00:05"
- service: homeassistant.update_entity
entity_id: sensor.smartweather_cccc
- delay: "{{ interval }}"
- service: script.smartweather_set_sensor_interval
Errors:
Error executing service <ServiceCall script.smartweather_set_sensor_interval (c:ea2f27a05309415da9bd38a574812dcb)>
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/core.py", line 1244, in _safe_execute
await self._execute_service(handler, service_call)
File "/usr/src/homeassistant/homeassistant/core.py", line 1261, in _execute_service
await handler.func(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 142, in service_handler
await script.async_turn_on(variables=service.data, context=service.context)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 214, in async_turn_on
raise err
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 209, in async_turn_on
await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 190, in async_run
await self._handle_action(action, variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 274, in _handle_action
await self._actions[_determine_action(action)](action, variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 357, in _async_call_service
context=context,
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 97, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context
File "/usr/src/homeassistant/homeassistant/core.py", line 1236, in async_call
await asyncio.shield(self._execute_service(handler, service_call))
File "/usr/src/homeassistant/homeassistant/core.py", line 1261, in _execute_service
await handler.func(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 139, in service_handler
if script.is_on:
AttributeError: 'NoneType' object has no attribute 'is_on'
-------------
Error executing script script.smartweather_set_sensor_interval. Unknown error for call_service at pos 1:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 209, in async_turn_on
await self.script.async_run(kwargs.get(ATTR_VARIABLES), context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 190, in async_run
await self._handle_action(action, variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 274, in _handle_action
await self._actions[_determine_action(action)](action, variables, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 357, in _async_call_service
context=context,
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 97, in async_call_from_config
domain, service_name, service_data, blocking=blocking, context=context
File "/usr/src/homeassistant/homeassistant/core.py", line 1236, in async_call
await asyncio.shield(self._execute_service(handler, service_call))
File "/usr/src/homeassistant/homeassistant/core.py", line 1261, in _execute_service
await handler.func(service_call)
File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 139, in service_handler
if script.is_on:
AttributeError: 'NoneType' object has no attribute 'is_on'