Issue with Automation / Config

I get this error when my automations look like this
Logger: homeassistant.config
Source: config.py:464
First occurred: 6:57:13 AM (3 occurrences)
Last logged: 6:57:20 AM

Invalid config for [automation]: Service switch.turn_{{‘on’ if trigger.id = ‘on’ else ‘off’ }} target: entity_id: switch.fan_controller does not match format . for dictionary value @ data[‘action’][0][‘service’]. Got None. (See /config/configuration.yaml, line 3).

- id: '1638223769335'
  alias: Fan Control
  description: Tun off fan in the morning, on at bedtime
  trigger:
  - platform: time
    at: 06:30:00
  - platform: time
    at: '20:30:00'
    id: 'on'
  condition: []
  action:
  - service: "switch.turn_{{'on' if trigger.id = 'on' else 'off' }} target:\n  entity_id:\
      \ switch.fan_controller\n"
  mode: single

and configuration looks like this

# Activate the configuration editor
config:

automation: !include automations.yaml

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
#http:
#base_url: wadsley.duckdns.org:8123
#  ssl_certificate: 78e65fad-9879-4d30-916e-f2ec94cd929c
#  ssl_key: /ssl/privkey.pem



# Text to speech
#tts:
#  - platform: google_translate
#
#group: !include groups.yaml
#automation: !include automations.yaml
#script: !include scripts.yaml
#scene: !include scenes.yaml
- id: '1638223769335'
  alias: Fan Control
  description: Tun off fan in the morning, on at bedtime
  trigger:
  - platform: time
    at: 06:30:00
    id: 'off'
  - platform: time
    at: '20:30:00'
    id: 'on'
  condition: []
  action:
  - service: "switch.turn_{{ trigger.id }}"
    target:
      entity_id: switch.fan_controller
  mode: single

EDIT

Alternative:

- id: '1638223769335'
  alias: Fan Control
  description: Tun off fan in the morning, on at bedtime
  trigger:
  - platform: time
    at:
    - '06:30:00'
    - '20:30:00'
  condition: []
  action:
  - service: "switch.turn_{{ 'on' if now().hour == 20 else 'off' }}"
    target:
      entity_id: switch.fan_controller
  mode: single

that’s wrong, your service line is missing the ending quote. Also, you can simplify this by removing the quotes because the field starts with a character and not {{. Lastly, just use the id field to select the service instead of the if statement.

- id: '1638223769335'
  alias: Fan Control
  description: Tun off fan in the morning, on at bedtime
  trigger:
  - platform: time
    at: 06:30:00
    id: 'off'
  - platform: time
    at: '20:30:00'
    id: 'on'
  condition: []
  action:
  - service: switch.turn_{{ trigger.id }} 
    target:
      entity_id: switch.fan_controller
  mode: single

Thank you. Is this something you just modified the yaml or can this be done in GUI?

I don’t use the ui. I think you can do it all. If it doesn’t open, then you’d have to fix it in yaml then edit from the ui

Modifying the YAML is often more likely to get you what you really want (unless the Automation Editor has other ideas when it saves it).

Ya, to help visualize what I’m trying to do, the editor is really helpful. I often can’t figure out how the yaml is broken when something doesn’t work…

Occasionally toggle between the two editing modes to develop an understanding of how whatever you create in UI mode is constructed in YAML mode. Eventually you’ll be able to compose it directly in YAML (which allows you to do a few things that aren’t supported in UI mode).

FWIW, I don’t use the Automation Editor and prefer to compose automations using a text editor (Microsoft VS Code). The Automation Editor, in UI mode, is fine for creating simple automations but gets in one’s way when complexity increases. It also has a few limitations that, once you encounter them, serve to make you seek a more flexible means of composing automations.

1 Like