How to use variables?

Hey folks, I’m new to HA and tried a little with variables. I have a simple motion activated light automation with two sensors. I want to reuse the timeout value in the entire script.

Is this even possible using variables?
I get these malformed error messages. Seems like the variable isn’t expanding correctly.

This is my current example:

alias: Neue Automatisierung
description: ""
variables:
  timeout: "00:03:00"
  minutes: 3
triggers:
  - type: motion
    device_id: b51290f7eaaba46e324681fd365f3af7
    entity_id: a6a49dcf03c5a5976762bda65e3848ca
    domain: binary_sensor
    trigger: device
    id: on1
  - type: motion
    device_id: 39ac8d5ebff099e9c4ea7329d84cf15b
    entity_id: 2357911109623a9d75806e4d229d6de8
    domain: binary_sensor
    trigger: device
    id: on2
  - type: no_motion
    device_id: b51290f7eaaba46e324681fd365f3af7
    entity_id: a6a49dcf03c5a5976762bda65e3848ca
    domain: binary_sensor
    trigger: device
    for:
      minutes: "{{ minutes }}" # Message malformed: expected float for dictionary value @ data['for']['minutes']
    id: off1
  - type: no_motion
    device_id: 39ac8d5ebff099e9c4ea7329d84cf15b
    entity_id: 2357911109623a9d75806e4d229d6de8
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: off2
conditions: []
actions:
  - alias: Ausschalten
    if:
      - condition: and
        conditions:
          - type: is_no_motion
            condition: device
            device_id: b51290f7eaaba46e324681fd365f3af7
            entity_id: a6a49dcf03c5a5976762bda65e3848ca
            domain: binary_sensor
            for:
              hours: 0
              minutes: 3
              seconds: 0
          - type: is_no_motion
            condition: device
            device_id: 39ac8d5ebff099e9c4ea7329d84cf15b
            entity_id: 2357911109623a9d75806e4d229d6de8
            domain: binary_sensor
            for:
              hours: 0
              minutes: 3
              seconds: 0
    then:
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.ganglicht
  - alias: Einschalten
    if:
      - condition: trigger
        id:
          - on2
          - on1
    then:
      - if:
          - condition: time
            after: "06:00:00"
            before: "22:00:00"
        then:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
            target:
              entity_id: light.ganglicht
        else:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 20
            target:
              entity_id: light.ganglicht
        alias: Lichtdetails
mode: single

I don’t think you can use templates with device IDs. Have you tried this using entity IDs instead?

3 Likes

I’m not sure what you mean. I don’t want to use variable/templates with device IDs. I want to reuse the timeout value of 3 min in the for. Is this even possible?

But my problem is solved without variables by grouping the sensors.

In the UI when you choose a trigger (or condition or action) don’t click the “device” option, which is the first choice in the dropdown.

1 Like