How to use variables in automaton (Message malformed)?

I’m trying to reference the helper value, but every attempt results in “Message malformed: expected float for dictionary value @ data[‘below’]”

Here is my configuration:

alias: Maciek - Air Humidifier - On
description: ""
triggers:
  - trigger: time_pattern
    minutes: /5
conditions:
  - condition: and
    conditions:
      - condition: time
        after: "06:00:00"
        before: "22:00:00"
        weekday:
          - sun
          - sat
          - fri
          - thu
          - wed
          - tue
          - mon
      - type: is_humidity
        condition: device
        device_id: 1149c9f9af2156ac06eff73cd8dfc1f1
        entity_id: 1bc89ca09440bef6615f410d359924f3
        domain: sensor
        below: {{states.input_number.air_humidifier_on}}
actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      device_id: 6d55b00f96bde882735687565ac4db25
mode: single

I’ve tried following combination without success:

below: {{states.input_number.air_humidifier_on}}
below: {{states(input_number.air_humidifier_on)}}
below: {{states(input_number.air_humidifier_on)|float}}
below: {{states('input_number.air_humidifier_on')|float}}
below: '{{states("input_number.air_humidifier_on")|float}}'
below: states.input_number.air_humidifier_on

What is the correct syntax?

Hello maciejmiklas,

The correct syntax to make this work is use an entity condition not a device condition. Device actions do not accept templates for the most part.

2 Likes

Thank you!
I’m migrating from:

      - condition: device
        type: is_off
        device_id: d9a17175829de22c66b90ad2877db4b8
        entity_id: 9371c57aed946b8081ca5855dabeb3a2
        domain: switch
        for:
          hours: 0
          minutes: 5
          seconds: 0

to:

      - condition: state
        entity_id: switch.tuya_plug_1_socket
        state: "off"
        for:
          hours: 0
          minutes: 5
          seconds: 0

However, I still have the same problem with variables. I’ve tried all combinations from above, and it always parses the expression as text.

Do I have to install something to be able to use variables, like GitHub - rogro82/hass-variables: Home Assistant variables component ?

Post one or two examples of what you tried but it failed to work.


For example, this is a valid Numeric State Condition.

  - condition: numeric_state
    entity_id: sensor.your_humidity_sensor
    below: input_number.air_humidifier_on

I’m trying to find a way to define global variable and reference it. I have in Helpers following numeric value: input_number.air_humidifier_on and would like to reference it:

alias: Artur  - Air Humidifier - On at {{HERE}} %
conditions:
      - condition: numeric_state
        entity_id: sensor.artur_temp_humidity
        below: {{HERE}}

Home Assistant doesn’t support the concept of global variables. It supports defining variables within an automation or script but they’re not global (i.e. cannot be referenced outside the automation where they’re defined).

The closest approximation to a global variable is a helper (such as Input Number, Input Text, etc).

A Numeric State Condition’s below option doesn’t support templates. Refer to the link I posted above to see what it does support.

You could install that custom integration. All the automations you create that use it will be forever dependent on it.

What exactly di you intend to store in a global variable? The entity_id of an Input Number helper?

I have five humidity sensors, one in each room. Each room also has a humidifier that should go on when humidity is below 43%. I’ve set automation for each room, and each yaml has a hardcoded value of 43. I would like to have the ability to set this value as a global variable, so when I change it, I have to do it in one place and do not have to edit five yam files.

I’ve read once again documentation, and this is the correct form:

      - condition: numeric_state
        entity_id: sensor.artur_temp_humidity
        below: input_number.air_humidifier_on

Interesting… I was sure the whole HA would support the same templating syntax, but that’s not the case. This was the source of my confusion!

Thank you for help!

1 Like

That’s right; it’s exactly like the valid Numeric State Condition example I provided in my previous post. Please consider marking it with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

1 Like