Template in automation trigger's "for" not working

Hi,

I’m trying to use a template for a “for” value in my trigger. I can’t seem to get it working.

I have:

type: no_motion
platform: device
[...]
for:
    hours: 0
    minutes: minutes: '{{"8" if now.hour() in [7, 8] else "2"}}'
    seconds: 0

I’m getting the following error:

Message malformed: expected float for dictionary value @ data['for']['minutes']

Happy for any help. Please excuse bad formatting I typed this on my phone.

Cheers

1 Like

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.

1 Like

Thanks for your suggestions @rccoleman - This is my final version:

type: no_motion
platform: device
[...]
for:
  hours: 0
  minutes: '{{ 8 if now().hour in [8, 9, 10] else 2 }}'
  seconds: 0 

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.

This works for me:

- alias: test
  initial_state: true
  trigger:
    - platform: state
      entity_id: input_boolean.test1
      for:
        seconds: '{{ 10 }}'
  action:
    - service: persistent_notification.create
      data:
        message: test

What are you doing when you get that error?

I’m trying to create an automation, the error occurs upon hitting the save button.

alias: Badezimmer Licht aus
description: ''
trigger:
  - type: no_motion
    platform: device
    device_id: 1234
    entity_id: binary_sensor.badezimmer
    domain: binary_sensor
    for:
      hours: 0
      minutes: '{{ 10 }}'
      seconds: 0
condition: []
action:
  - service: light.turn_off
    data: {}
    entity_id: light.badezimmer
mode: single

This is my full automation. Still receiving the same error…

1 Like

I pasted your automation into my automations.yaml file and it passed “Check Configuration”.

Are you using the Automation Editor to create this automation?

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

Welcome to the HA forums!

Can you post your automation? According to the docs, templates can be used in the for: statement. So maybe it’s something else.

Thanks for the reply @KSC, here is what I got…

alias: Motion activated light (office)
  description: ''
  trigger:
  - platform: device
    domain: binary_sensor
    entity_id: binary_sensor.tradfri_motion_sensor
    device_id: 0a4565a7835e24ba258831901ae824d1
    type: motion
  - type: no_motion
    platform: device
    device_id: 0a4565a7835e24ba258831901ae824d1
    entity_id: binary_sensor.tradfri_motion_sensor
    domain: binary_sensor
    for:
      hours: 0
      minutes: '{{ 10 if (now().hour >= 7 and now().hour <= 16) else 1 }}'
      seconds: 0
  condition: []
  action:
  - service_template: 'light.turn_{{states(''binary_sensor.tradfri_motion_sensor'')}}'
    entity_id: light.office_leds

I’ve thoroughly tested the rest of this script, the only thing that doesn’t work is the

 minutes: '{{ 10 if (now().hour >= 7 and now().hour <= 16) else 1 }}'

When trying to save in the automation’s editor, I get the error
“Message malformed: expected float for dictionary value @ data[‘for’][‘minutes’]”

Same happens if I try casting to a float with…

'{{ 10 if (now().hour >= 7 and now().hour <= 16 else 1) | float }}'

I would really appreciate any guidance that can be given. Thanks!

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.

1 Like

Please don’t.

  • If you don’t see me participating in a topic, chances are it’s because I’m not interested.
  • Tagging is discouraged in guideline #16:

Yea, well, I don’t know how to report a bug. Figured one of you would. And if you don’t want to help, that’s okay. Ignore it.

If you have a free GitHub account, you can open a new Issue in the Core repository.

Got it. Thanks!

Thanks @KSC , that is a clever thing to try. Switching to use the state platform did the trick!

Thanks for the confirmation, @wired. I’m submitting a new issue on that one in the repository. Glad it’s working now!

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.