YAML sanity check please

I am just getting into YAML - day 2, so any guidance appreciated.

Leaning hard into ChatGPt to get me started :slight_smile:

Basically need to do this -

sensor.blind_master_battery entity goes below 25 or above 80, and then it turns on or off the switch.master_sockets_window_right accordingly.

- alias: "Control Master Window Right Socket based on Blind Battery"
  trigger:
    - platform: numeric_state
      entity_id: sensor.blind_master_battery
      below: 25
    - platform: numeric_state
      entity_id: sensor.blind_master_battery
      above: 80
  action:
    - service: >
        {% if trigger.to_state.state | int < 25 %}
          switch.turn_on
        {% elif trigger.to_state.state | int > 80 %}
          switch.turn_off
        {% endif %}
      entity_id: switch.master_sockets_window_right

And when saving I get an error “Message malformed: extra keys not allowed @ data[‘0’]”

Any direction/guidance appreciated.

I am testing it in Developer Tools /Template and I am getting a ‘trigger’ is undefined’ error

The variable trigger has no value in the Template editor, unless you assign one to it.

You can use Trigger IDs to simplify your automation, and you need to place the entity ID under target or data (this is the source of the Message malformed error):

- alias: "Control Master Window Right Socket based on Blind Battery"
  trigger:
    - platform: numeric_state
      entity_id: sensor.blind_master_battery
      below: 25
      id: 'on'
    - platform: numeric_state
      entity_id: sensor.blind_master_battery
      above: 80
      id: 'off'
  condition: []
  action:
    - service: switch.turn_{{ trigger.id }}
      target: 
        entity_id: switch.master_sockets_window_right

FWIW, ChatGPT and other LLM are particularly bad at Home Assistant. They hallucinate answers the majority of the time. And their hallucinations are often plausible-looking, making them difficult to identify without significant HA experience.

Another tip for using templates in automations – use the UI to create the action first using placeholder values before going back to add the templates. This makes sure you get the structure of the YAML correct and would have avoided the error of not having entity_id under target in this case.