How do I get my automation to work with the schedule for my security light plug?

Hey there,

I know it’s probably going to be something very simple, I am out of ideas to what it could be.I’ve already made the schedule and used the code from the Original post and tried to work out what the errors by referring to this post about errors. however I’m at a loss for what is wrong with it as it is still not working.

Sorry if I haven’t done something that is very obvious. Please let me know what it is. Could it be something that I have not made like the service in my configuration.yaml file.

Thank you.

Home Assistant 2022.10.5 (Container)

The goal of my automation is to be able to have the plug turn on and off depending on the schedule helper in the home assistant.

My Automation code

alias: example
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.hallway_light_schedule
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state in ['on', 'off'] }}"
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.hall_light_plug_light

Changes that I made from the original code are

  • from schedule.your_schedule to schedule.hallway_light_schedule|
  • from light.your_light to light.hall_light_plug_light|
  • from ‘light.turn_{{ trigger.to_state.state }}’ to light.turn_{{ trigger.to_state.state }}
    (I’ve tried putting back the ’ and then run the automation it does not keep them in, also I tried “” and the same thing happens)

I get these errors when I run this automation from the logs screen

Errors


Logger: homeassistant.helpers.template
Source: helpers/template.py:420
First occurred: 18:40:59 (8 occurrences)
Last logged: 19:49:46

Template variable error: 'dict object' has no attribute 'to_state' when rendering 'light.turn_{{ trigger.to_state.state }}'
Template variable error: 'trigger' is undefined when rendering 'light.turn_{{ trigger.to_state.state }}'

Logger: homeassistant.components.automation.example
Source: helpers/script.py:409
Integration: Automation (documentation, issues)
First occurred: 18:40:59 (7 occurrences)
Last logged: 19:49:46

example: Error executing script. Error for call_service at pos 1: Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'

Logger: homeassistant.components.automation.example
Source: components/automation/__init__.py:253
Integration: Automation (documentation, issues)
First occurred: 18:40:59 (7 occurrences)
Last logged: 19:49:46

Error while executing automation automation.example: Error rendering service name template: UndefinedError: 'dict object' has no attribute 'to_state'	

The Hall Light Plug Light entity State attributes

supported_color_modes:
- onoff
off_with_transition: false
off_brightness: null
friendly_name: Hall Light Plug Light
supported_features: 8

That’s why there are errors complaining that there’s no to_state attribute and the trigger variable is undefined.

The trigger variable, referenced by the templates in the condition and service call, is defined when the automation is triggered by its State Trigger. If you test the automation any other way, the trigger variable is undefined (and the errors you got are reported)

Reference: Testing your automation

A bit of an update the day after the Last Post (Oct 26 2022) the automation was working and I have no idea why it was as seen in the history log below with the schedule automation which it follows

History Timeline

Schedule

then something happened to cause the container to restart which meant home assistant started in safe mode and it was saying that the code below needed to be commented out to get home assistant working correctly (this code was located in my configuration.yaml file).

service: light.turn_{{ trigger.to_state.state }}
  target:
    entity_id: light.hall_light_plug_light

Once I did that home assistant started working correctly however the automation has stopped working now and I get this error message from the repair section.

Error message on Home Assistant

image

Any ideas on this? as I am confused about this now

Now running Home Assistant 2022.11.1 (Container)