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

I’m trying to template a time in the wait_for_trigger action

Here’s the whole automation for reference

alias: Pantry Light - delayed timer
description: Controls the Pantry light and turns if off after xx time
trigger:
  - type: motion
    platform: device
    device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
    entity_id: 7ee585329c2b079ed28428fb2e0cf50b
    domain: binary_sensor
condition: []
action:
  - service: adaptive_lighting.apply
    data:
      entity_id: switch.adaptive_lighting_pantry_storeroom
      lights:
        - light.pantry_light
      transition: "1"
      adapt_brightness: true
      turn_on_lights: true
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
        entity_id: 7ee585329c2b079ed28428fb2e0cf50b
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 10
    timeout:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - type: turn_off
    device_id: ccb23a71382bdd7150b846251db03989
    entity_id: 39b084358cb59aad38026c03cdb2f263
    domain: light
  - service: counter.increment
    target:
      entity_id: counter.pantry_toggle
    data: {}
  - delay:
      seconds: 10
  - service: counter.reset
    target:
      entity_id: counter.pantry_toggle
    data: {}
mode: restart

The issue i’m having is it won’t accept a templated entry for this part of it

  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
        entity_id: 7ee585329c2b079ed28428fb2e0cf50b
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 10

Every time i try to put in something it says…
Message malformed: expected dict for dictionary value @ data[‘for’]

I’ve tried basics like this…

{% set blah = 22 %}
{% set my_dict = {
  "hours": 0,
  "minutes": 0,
  "seconds": blah
}%}

{{ my_dict }}

and still doesn’t work. Ideally I want to convert xxxx seconds to ‘00:01:20’ and put it in for: , but it doesn’t even accept that .

Script Syntax - Home Assistant seems to indicate even basic values should work eg for: 10, but i get the same error.

My end goal is simple, and perhaps i’m overcomplicating it… but a real example will be
Motion detect 1 : light on 20s
light turns off, but then detects motion again within 10s
Motion detect 2 : light on 20 * 2+2
Motion detect 3 : light on 20 * 4+2
Motion detect 4 : light on 20 * 5+2

I think you get the idea, and there is an upper limit

logic something like this

        for:
          hours: 0
          minutes: 0
          seconds: >
            {% set base_duration = 20 %}
            {% set counter_value = state_attr('counter.pantry_toggle', 'value') | int %}
            {% set max_duration = 180 %}
            {{ min(base_duration * (counter_value + 1) + 2, max_duration) }}

for should accept a seconds value directly like:

    for: >
      {% set base_duration = 20 %}
      {% set counter_value = state_attr('counter.pantry_toggle', 'value') | int(0) %}
      {% set max_duration = 180 %}
      {{ min(base_duration * (counter_value + 1) + 2, max_duration) }}

But I doubt this automation will work as you seem to intend it to work. The mode is set to restart, and both the main trigger and wait trigger are are listening for the same event… so the whole automation will start over when the event occurs. Nothing after the wait will be executed unless the wait times out.

You will likely be better off just using a simpler automation with two triggers instead of a convoluted wait:

alias: Pantry Light - delayed timer
description: Controls the Pantry light and turns if off after xx time
trigger:
  - id: 'on'
    type: motion
    platform: device
    device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
    entity_id: 7ee585329c2b079ed28428fb2e0cf50b
    domain: binary_sensor
  - id: 'off'
    type: no_motion
    platform: device
    device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
    entity_id: 7ee585329c2b079ed28428fb2e0cf50b
    domain: binary_sensor
    for:
      minutes: 3
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'on'
        sequence:
          - condition: state
            entity_id: light.pantry_light
            state: 'off'
          - service: adaptive_lighting.apply
            data:
              entity_id: switch.adaptive_lighting_pantry_storeroom
              lights:
                - light.pantry_light
              transition: "1"
              adapt_brightness: true
              turn_on_lights: true
    default:
      - type: turn_off
        device_id: ccb23a71382bdd7150b846251db03989
        entity_id: 39b084358cb59aad38026c03cdb2f263
        domain: light
mode: restart

Thanks for the input, 2 triggers is 100% easier!

As for the for: being dynamic… its just not accepting anything… here’s what i’ve tried - just keeping it really simple.

type: no_motion
platform: device
device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
entity_id: 7ee585329c2b079ed28428fb2e0cf50b
domain: binary_sensor
for:
  hours: 0
  minutes: 0
  seconds: >
    {{ 10 | float }}
id: Motion Stopped

Error is still: Message malformed: expected float for dictionary value @ data[‘for’][‘seconds’]

most things i’ve tried don’t work so it seems like having a dynamic duration in here isn’t supported?

I tried a different approach and it worked - for reference here’s the whole automation

1 trigger - 10s
2nd 45s
3rd 90s

If you walk in and out within the 90s it won’t extent the time… its only if the light turns off and you turn it back on within 10s then it extends the time.

alias: Pantry - 2 triggers
description: Controls the Pantry light and turns if off after xx time
trigger:
  - type: motion
    platform: device
    device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
    entity_id: 7ee585329c2b079ed28428fb2e0cf50b
    domain: binary_sensor
    enabled: true
    id: Motion Detected
  - type: no_motion
    platform: device
    device_id: a2f3426f9ebd8ccb8a486a2ff8e107b7
    entity_id: 7ee585329c2b079ed28428fb2e0cf50b
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
    id: Motion Stopped
condition: []
action:
  - if:
      - condition: trigger
        id:
          - Motion Detected
    then:
      - service: adaptive_lighting.apply
        data:
          entity_id: switch.adaptive_lighting_pantry_storeroom
          lights:
            - light.pantry_light
          transition: "1"
          adapt_brightness: true
          turn_on_lights: true
    alias: Trigger - Motion Detected
  - alias: Trigger - Motion stopped
    if:
      - condition: trigger
        id:
          - Motion Stopped
    then:
      - delay: >-
          {% set counter_value = states('counter.pantry_toggle') | int(0) %} {{
          as_timedelta(min(15*(counter_value)*3, 180) | string ) }}
      - service: counter.increment
        target:
          entity_id: counter.pantry_toggle
        data: {}
      - type: turn_off
        device_id: ccb23a71382bdd7150b846251db03989
        entity_id: 39b084358cb59aad38026c03cdb2f263
        domain: light
      - delay:
          seconds: 10
      - service: counter.reset
        target:
          entity_id: counter.pantry_toggle
        data: {}
mode: restart

Because you’re using a Device Trigger and it doesn’t support templates.

Change it to a State Trigger.

platform: state
entity_id: binary_sensor.your_binary_sensor
from: 'on' 
to: 'off' 
for:
  seconds: >
    {{ 10 | float }}
1 Like

Thanks, that was it! I got there in the end :slight_smile: thanks all for the guidance