Hello group,
I’m trying to pass a duration to a timer by accessing the attributes of a state object passed in to the script below from an automation. I’ve included the script below.
alias: Auto Light Motion Detected Script
description: Common mode control
fields:
arg_binary_sensor:
description: The sensor entity to act on
example: binary_sensor.front_porch_cam_motion
sequence:
- condition: and
conditions:
- '{{ is_state(state_attr(arg_binary_sensor, ''enable_entity''), ''on'')}}'
- '{{ is_state(state_attr(arg_binary_sensor, ''timer_entity''), ''idle'')}}'
- '{{ is_state(state_attr(arg_binary_sensor, ''switch_entity''), ''off'')}}'
- condition: or
conditions:
- condition: state
entity_id: sun.sun
state: below_horizon
- '{{ is_state(state_attr(arg_binary_sensor, ''daytime_enable''), ''on'')}}'
- service: timer.start
data:
duration: '{{ state_attr(arg_binary_sensor, ''duration'') }}'
target:
entity_id: '{{ state_attr(arg_binary_sensor, ''timer_entity'') }}'
- service: switch.turn_on
data: {}
target:
entity_id: '{{ state_attr(arg_binary_sensor, ''switch_entity'') }}'
mode: queued
icon: mdi:lightbulb-auto
max: 10
This line is failing in the trace:
duration: '{{ state_attr(arg_binary_sensor, ''duration'') }}'
The error message in the log is:
Logger: homeassistant.components.script.auto_light
Source: helpers/script.py:409
Integration: Script (documentation, issues)
First occurred: 1:23:33 AM (1 occurrences)
Last logged: 1:23:33 AM
Logger: homeassistant.components.script.auto_light
Source: helpers/script.py:409
Integration: Script (documentation, issues)
First occurred: 1:23:33 AM (1 occurrences)
Last logged: 1:23:33 AM
Auto Light Motion Detected Script: Error executing script. Invalid data for call_service at pos 2: offset input_number.systemvariabledurationdrivewayfloodsmotion should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['duration']
Note that I recently changed the script to use the state attribute variable passed in from an automation instead of the reference to a common input number. Here is a diff of the change:
- service: timer.start
data:
- duration: '{{states(''input_number.durationlightsmotiondetect'')}}'
+ duration: '{{ state_attr(arg_binary_sensor, ''duration'') }}'
target:
entity_id: '{{ state_attr(arg_binary_sensor, ''timer_entity'') }}'
- service: switch.turn_on
Regarding the change noted above, the previous version used a global input_number helper variable, and its value was in seconds.
When I use the state attribute, the error message seems to indicate it wants HH:MM, ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’
The documentation for timer says it can accept seconds only as an argument for duration:
Duration in seconds or 00:00:00 until the timer finishes.
I’m puzzled as to why it is throwing an error on the state attribute version.
Any help would be appreciated.
Thanks,
Steve