Expected float for dictionary value

I have two scripts one sets a threshold and then calls the other. I’m getting an error in the second one that I can’t seem to get fixed, I’ve tried a few things I’ve seen on the forums here but nothing has worked yet.
Here’s the error message in the log:
Logger: homeassistant.components.script.humcontrolstep2
Source: helpers/script.py:1138
Integration: Script (documentation, issues)
First occurred: 4:09:58 PM (24 occurrences)
Last logged: 8:27:26 PM

HumControlStep2: Error executing script. Invalid data for choose at pos 1: expected float for dictionary value @ data[‘above’]

Here are both the scripts that:


humidifier_control_logic:
  alias: Humidifier Control Logic
  sequence:
  - variables:
      th: '{% set t = state_attr(''weather.wingate'', ''temperature'') | float %}
        {% if t < 0 %} [30, 20] {% elif t < 10 %} [35, 25] {% elif t < 20 %} [40,
        30] {% elif t < 40 %} [45, 35] {% else %} [65, 50] {% endif %}

        '
  - service: script.turn_on
    entity_id: script.humcontrolstep2
    data:
      variables:
        maxhumidity: '{{ th[0] }}'
        minhumidity: '{{ th[1] }}'
  mode: single
humcontrolstep2:
  alias: HumControlStep2
  fields:
    maxhumidity:
      description: Maximum humidity level that will be tolerated in any active zone
      example: 65
    minhumidity:
      description: Minimum humidity level that will trigger humidifier in any active
        zone
      example: 55
  sequence:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - condition: and
          conditions:
          - condition: device
            device_id: 798e40f56aa4cfc8560fb105935b7aef
            domain: climate
            entity_id: climate.downstairs
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 798e40f56aa4cfc8560fb105935b7aef
            entity_id: sensor.downstairs_humidity
            domain: sensor
            above: '{{ maxhumidity | float }}'
            below: 150
        - condition: and
          conditions:
          - condition: device
            device_id: 4348095d4f3d75c83b9a7676850fa6ba
            domain: climate
            entity_id: climate.living_room
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 4348095d4f3d75c83b9a7676850fa6ba
            entity_id: sensor.living_room_humidity
            domain: sensor
            above: '{{ maxhumidity | float }}'
            below: 150
        - condition: and
          conditions:
          - condition: device
            device_id: 4171867aea6553495a18c621f27bb894
            domain: climate
            entity_id: climate.master_bedroom
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 4171867aea6553495a18c621f27bb894
            entity_id: sensor.master_bedroom_humidity
            domain: sensor
            above: '{{ maxhumidity | float }}'
            below: 150
      sequence:
      - type: turn_off
        device_id: 1f45c77b73dcbaeb59895017affe7225
        entity_id: switch.humidifier_control
        domain: switch
    - conditions:
      - condition: or
        conditions:
        - condition: and
          conditions:
          - condition: device
            device_id: 798e40f56aa4cfc8560fb105935b7aef
            domain: climate
            entity_id: climate.downstairs
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 798e40f56aa4cfc8560fb105935b7aef
            entity_id: sensor.downstairs_humidity
            domain: sensor
            below: '{{ minhumidity | float }}'
        - condition: and
          conditions:
          - condition: device
            device_id: 4348095d4f3d75c83b9a7676850fa6ba
            domain: climate
            entity_id: climate.living_room
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 4348095d4f3d75c83b9a7676850fa6ba
            entity_id: sensor.living_room_humidity
            domain: sensor
            below: '{{ minhumidity | float }}'
        - condition: and
          conditions:
          - condition: device
            device_id: 4171867aea6553495a18c621f27bb894
            domain: climate
            entity_id: climate.master_bedroom
            type: is_hvac_mode
            hvac_mode: heat
          - type: is_humidity
            condition: device
            device_id: 4171867aea6553495a18c621f27bb894
            entity_id: sensor.master_bedroom_humidity
            domain: sensor
            below: '{{ minhumidity | float }}'
      sequence:
      - type: turn_on
        device_id: 1f45c77b73dcbaeb59895017affe7225
        entity_id: switch.humidifier_control
        domain: switch
    default:
    - type: turn_off
      device_id: 1f45c77b73dcbaeb59895017affe7225
      entity_id: switch.humidifier_control
      domain: switch
  mode: single

Replace this:

with this:

sequence:
- variables:
  th: >
    {% set t = state_attr("weather.wingate", "temperature") | float %}
    {% if t < 0 %} [30, 20]
    {% elif t < 10 %} [35, 25]
    {% elif t < 20 %} [40, 30]
    {% elif t < 40 %} [45, 35]
    {% else %} [65, 50]
    {% endif %}

You can’t use variables with device conditions, change them to state conditions.

I have streamlined script.humcontrolstep2 so that it no longer relies on Device Conditions.

You will have to check and confirm the condition logic is correct because it was a bit challenging to decipher it from the original script.

humcontrolstep2:
  alias: HumControlStep2
  fields:
    maxhumidity:
      description: Maximum humidity level that will be tolerated in any active zone
      example: 65
    minhumidity:
      description: Minimum humidity level that will trigger humidifier in any active
        zone
      example: 55
  sequence:
  - variables:
      downstairs_heat: "{{ is_state('climate.downstairs', 'heat') }}"
      downstairs: "{{ states('sensor.downstairs_humidity') | float }}"
      living_heat: "{{ is_state('climate.living_room', 'heat') }}"
      living: "{{ states('sensor.living_room_humidity') | float }}"
      bedroom_heat: "{{ is_state('climate.master_bedroom', 'heat') }}"
      bedroom: "{{ states('sensor.master_bedroom_humidity') | float }}"
  - choose:
    - conditions: >
        {{  (downstairs_heat and downstairs < 150 and downstairs > maxhumidity) or 
            (living_heat and living < 150 and living > maxhumidity) or 
            (bedroom_heat and bedroom < 150 and bedroom > maxhumidity)  }}
      sequence:
      - service: switch.turn_off
        entity_id: switch.humidifier_control
    - conditions: >
        {{  (downstairs_heat and downstairs < minhumidity) or 
            (living_heat and living < minhumidity) or 
            (bedroom_heat and bedroom < minhumidity)  }}
      sequence:
      - service: switch.turn_on
        entity_id: switch.humidifier_control

The variables minhumidity and maxhumidity are passed to the script as float values (not string values) so there’s no need to convert them with the float filter as was done in the original script.

1 Like

Out of curiosity, how did you create a Device Condition containing a template?

Did you switch the Automation Editor into YAML mode in order to add the template?

The reason why I ask is because the Automation Editor doesn’t support a template where you put one in the Device Condition. For example, if I do this:

Screenshot from 2021-02-05 10-40-46

and then switch to YAML mode, the text I entered is converted to .nan (Not A Number)

Screenshot from 2021-02-05 10-41-05

yup - I started in the editor and switched to configurator and updated scripts.yaml directly

For future reference, Device Triggers, Device Conditions, and Device Actions were designed to be used exclusively with the Automation Editor in UI mode. If you switch to YAML mode and start tinkering with them, there’s a good chance you’ll be trying to make it do something it refuses to allow in UI mode.

You may have also noticed that Device Trigger/Condition/Action are convenient to use but produce verbose code. For example, compare turning on a switch in your original script to what I posted. In practice, the additional complexity shouldn’t matter because you wouldn’t normally have to look at the generated code. However, you’re not the first person who has not only looked at the wordy code, of a Device Trigger/Condition/Action, but has also tried to change it (usually with bad results).

If you want to modify an automation in YAML mode, stick to using traditional State, Numeric_State, Template, etc triggers and conditions (i.e. anything else but Device-oriented).

2 Likes