Script: Message malformed

Hi everybody,

I want to create a script that will switch radiators, depending on the window state. This is what I have:

alias: Heizungen/Fenstercheck
description: "Fenster auf, Heizung aus; und umgekehrt"
variables:
  fenster: binary_sensor.oben_alles_fensterhelper_schlafzimmer
  heizung: climate.oben_schlafzimmer
sequence:
  - if:
      - condition: state
        entity_id: "{{ fenster }}"
        state: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 5
        alias: Fenster Auf
    then:
      - service: climate.set_hvac_mode
        data:
          hvac_mode: "off"
        target:
          entity_id: "{{ heizung }}"
        alias: HVAC Mode AUS
    alias: Fenster Auf
mode: single
icon: mdi:radiator

I first had this script in a yaml file. When I ran the configuration check, it gave me no errors, but upon reloading, the script would not appear anywhere. So I created it via the scripts editor.

There I get this error when trying to save the script Message malformed: Entity {{ fenster }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][0]['if'][0]['entity_id'].

Can you please help me fix this? Currently, countless automations have this code and I would prefer outsourcing it to one single script that all these automations can execute instead.

I was planning to call this script via automation and pass the appropriate fenster and heizung variables via the automations themselves.

It looks a bit weird here when I pasted the code, but indentations should all be correct in my original script:

Thank you for your help :slight_smile:

You can’t template the entity_id field for a state condition.

- condition: template
  value_template: >
    {% set item = states[fenster] %}
    {{ item.state == 'on' and (now() - item.last_updated) > timedelta(seconds=5) }}

Thank you. Can you please tell me, how the entire script would have to look? I tried the code below, and got this error In 'template' condition: TemplateError: Invalid domain name ''.

alias: Heizungen/Fenstercheck
description: Fenster auf, Heizung aus; und umgekehrt
variables:
  fenster: binary_sensor.oben_alles_fensterhelper_schlafzimmer
  heizung: climate.oben_schlafzimmer
sequence:
  - if:
      - condition: template
        value_template: | # Home Assistant automatically changed > to | after I saved this script
          {% set item = states[fenster] %}  {{ item.state == 'on' }}
    then:
      - service: persistent_notification.create
        data:
          message: test
mode: single
icon: mdi:radiator

You can’t test template conditions with the test button. Also, why aren’t you using the timing? Your previous condition had a for 5 seconds. Why did you remove it?

I thought the time part might have caused an issue, so I just removed it temporarily for testing.

The script looks like this now. I can save it, and run it. There is no visible error. But the logs say

Error in 'if[0]' evaluation: In 'template' condition: TemplateError: Invalid domain name ''
Error in 'if[0]' evaluation: In 'template' condition: UndefinedError: 'item' is undefined
alias: Heizungen/Fenstercheck
description: Fenster auf, Heizung aus; und umgekehrt
variables:
  fenster: binary_sensor.oben_alles_fensterhelper_schlafzimmer
  heizung: climate.oben_schlafzimmer
sequence:
  - if:
      - condition: template
        value_template: >
          {{ item.state == 'on' and (now() - item.last_updated) >
          timedelta(seconds=5) }}
    then:
      - service: notify.persistent_notification
        data:
          message: Fenster Auf, Heizung Aus
      - service: climate.set_hvac_mode
        data:
          hvac_mode: "off"
        target:
          entity_id: "{{ heizung }}"
    alias: Fenster Auf
  - if:
      - condition: template
        value_template: >
          {{ item.state == 'off' and (now() - item.last_updated) >
          timedelta(seconds=5) }}
    then:
      - service: notify.persistent_notification
        data:
          message: Fenster Zu, Heizung Auto
      - service: climate.set_hvac_mode
        data:
          hvac_mode: auto
        target:
          entity_id: "{{ heizung }}"
    alias: Fenster Zu
mode: single
icon: mdi:radiator

And the automation looks like this

alias: Heizungen/Fenstercheck
description: ""
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - service: script.heizungen_fenstercheck
    data:
      variables:
        fenster: binary_sensor.oben_alles_fensterhelper_schlafzimmer
        heizung: climate.oben_schlafzimmer
mode: single

because you left out the first line… The whole template is:

compared to what you wrote…

1 Like

D’oh! Yeah, it’s been a long day… everything seems to work now, thank you :slight_smile:

1 Like