Script puzzle - stuck! [variables within if/then]

Hi Community,

I’m stuck with a script problem; I’ve written what I think is valid syntax but I’m getting an error when I try to save the script and nothing I’ve tried resolves it.

The issue seems to be with the ‘then:’ clause of a if-then-else block. My syntax looks, as far as I can see, the same as examples in the HA documentation, so I’m a bit stumped by the error.

The error is this:

  • Message malformed: Unable to determine action @ data[‘sequence’][0][‘choose’][3][‘sequence’][1][‘then’][0]

The script looks like this:

alias: MVHR fan speed set
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.mvhr_control_mode
            state: "OFF"
        sequence:
          - type: turn_off
            device_id: 2e18b4caaecc076d728e18cb34326705
            entity_id: switch.vent_system
            domain: switch
          - type: turn_off
            device_id: 2e18b4caaecc076d728e18cb34326705
            entity_id: switch.vent_system_2
            domain: switch
      - conditions:
          - condition: state
            entity_id: input_select.mvhr_control_mode
            state: Purge
        sequence:
          - type: turn_on
            device_id: 2e18b4caaecc076d728e18cb34326705
            entity_id: switch.vent_system
            domain: switch
          - type: turn_on
            device_id: 62140836650d4002b81e4c5b4336be0c
            entity_id: light.vent_speed
            domain: light
            brightness_pct: 81
      - conditions:
          - condition: state
            entity_id: input_select.mvhr_control_mode
            state: Manual
        sequence:
          - type: turn_on
            device_id: 2e18b4caaecc076d728e18cb34326705
            entity_id: switch.vent_system
            domain: switch
      - conditions:
          - condition: state
            entity_id: input_select.mvhr_control_mode
            state: Auto
        sequence:
          - type: turn_on
            device_id: 2e18b4caaecc076d728e18cb34326705
            entity_id: switch.vent_system
            domain: switch
          - if:
              - alias: daytime
                condition: time
                after: "06:30:00"
                before: "22:30:00"
            then:
              - variables:
                  fan_base: 45
                  mult: 2
                  fan_speed: >-
                    {{ fan_base + mult * (
                    states('sensor.mvhr_sensors_air_temperature_3') -
                    state_attr('climate.wiser_lounge', 'current_temperature') ) }}
              - service: light.turn_on
                  data:
                    brightness_pct: "{{ fan_speed }}"
                  target:
                    entity_id: light.vent_speed
            else:
              - variables:
                  fan_base: 30
                  mult: 1
                  fan_speed: >-
                    {{ fan_base + mult * (
                    states('sensor.mvhr_sensors_air_temperature_3') -
                    state_attr('climate.wiser_lounge', 'current_temperature') ) }}
                - service: light.turn_on
                  data:
                    brightness_pct: "{{ fan_speed }}"
                  target:
                    entity_id: light.vent_speed
mode: single
icon: mdi:fan-chevron-up

Any idea why I’m getting that error? Probably a newbie error, but I can’t see it for looking!

Thanks!

You need to convert your states, which are strings, to numbers to perform mathematical operations. e.g.

                  fan_speed: >-
                    {{ fan_base + mult * (
                    states('sensor.mvhr_sensors_air_temperature_3')|float(0) -
                    state_attr('climate.wiser_lounge', 'current_temperature')|float(0) ) }}

The attribute may already be a number (attributes can be any valid type), but better safe than sorry. The state definitely isn’t though. They are always strings.

One final suggestion, stop using device actions. What happens if I have to replace a device - #2 by tom_l

Good to ensure the states are floats, I agree; and the device actions - bit of tidying up I’ll do later.

Adding those checks doesn’t help with the error that’s blocking me, though; it won’t even allow me to save the script right now. Any idea what’s causing the error with the ‘then’ clause?

Solved it! With the benefit of a fresh look - the data & target keys under the then clause “service” block were indented. Apparently they shouldn’t be. I am reminded once again that there’s a reason I dislike YAML!