I suspect that you want now().hour, not now.hour(). Also, you’re quoting the numbers, which turns them into strings, so remove the quotes around 8 and 2. 0.117 allows templates to return native types as long as you specify legacy_templates: false under homeassistant:, so as long as your expression evaluates to an integer, it should be fine.
And probably a problem with your phone text entry, but you’ve repeated minutes: there.
However, I still receive the same error Message malformed: expected float for dictionary value @ data['for']['minutes']. I’m on the latest 0.117.1 version and I even made sure to set legacy_templates: false.
I suspect that’s actually a bug on Home Assistant, since even minutes: '{{ 8 }}' outputs the same error.
Using the automation editor,I too receive this error. Additionally, I’ve tried just making this change in the automations.yaml file directly, and reloading the automations, but it too does not accept templating in this field
Hi, @wired - this is weird. It works in the developer/template tool. I tried all kinds of perturbations ending with this one (that still didn’t work - but stick with me):
for:
hours: 0
minutes: >
{% set long_wait = 10.0|float %}
{% set short_wait = 1.0|float %}
{% if (now().hour >= 7 and now().hour <= 16) %} {{long_wait|float}}
{% else %} {{short_wait|float}}
{% endif %}
seconds: 0
As you can see, I made everything a float every opportunity I had. Still received the same error as you did when reloading the automations (but not in the configuration check).
On a whim, I changed the trigger section to:
- alias: Motion activated light (office)
description: ''
trigger:
- platform: state
entity_id: binary_sensor.master_bedroom_sensor_motion
to: 'on'
- platform: state
entity_id: binary_sensor.master_bedroom_sensor_motion
to: 'off'
for:
hours: 0
minutes: >
{% set long_wait = 10.0|float %}
{% set short_wait = 1.0|float %}
{% if (now().hour >= 7 and now().hour <= 16) %} {{long_wait|float}}
{% else %} {{short_wait|float}}
{% endif %}
seconds: 0
condition: []
action:
- service_template: 'light.turn_{{states(''binary_sensor.master_bedroom_sensor_motion'')}}'
entity_id: light.hue_color_lamp_2
All I did was change the platform to state and then check for on and off conditions. It worked as it should.
Then I reverted the template to what you originally had (everything else the same as directly above):
{{ 10 if (now().hour >= 7 and now().hour <= 16) else 1 }}
And that worked, too. So… There must be a bug of some kind in the device platform. I’m tagging a couple others to help (@pnbruckner, @123, @frenck) see where we go from here. But the above will work - just not with the device platform.
It’s interesting to note that the for option is documented for two triggers: State Trigger and Numeric State Trigger. There’s no documentation at all for a Device Trigger.
Why not? Because it was invented exclusively for the use of the Automation Editor. When you examine its structure, there’s no easy way to create one manually because acquiring device_id isn’t easy (easy for the Automation Editor, not users).
The extent of what can be done with a Device Trigger is revealed in the Automation Editor … and templating for (called Duration in the UI) is not one of those revealed abilities. Editing a Device Trigger’s YAML is discouraged especially if you append options that aren’t available in the UI.
tl;dr
Don’t expect more from a Device Trigger than what is shown in the Automation Editor.