Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'

Hi, this is my first post, and first I will thank you all for HA, and the great community and help that is provided here, in other threads.

I am totally a newbie, I am starting with some automations, and I’ve been playing with the HACS integratiosn, Anniversaries and Garbage and I am testing with some dates to be notified about, and instead of creating an automation for every date, I am trying to do it all in one.

This is what I am tested, that I’ve seen in the author’s github:

automation:
  - alias: Anniversary Automation
    trigger:
    - platform: state
      entity_id: 
        - sensor.anniversary_first_sensor
        - sensor.anniversary_second_sensor
        - sensor.anniversary_third_sensor
      to: '1'
    action:
      - service: persistent_notification.create
        data:
          message: '{{trigger.to_state.attributes.friendly_name}} is tomorrow!'

But that returns the error:

Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'

I’m using telegram notify service but I imagine is doesn’t matter.

I search many threads, but it’s no working any of the solutions posted.

Any help would be really appreciated.

Thank you.

1 Like

I’m guessing you’re triggering this from the UI? If yes, that will error because there’s no trigger. Wait for the automation to occur naturally or use a test entity to trigger the automation.

You can use an input_number helper as a test entity.

2 Likes

Oh my god… It was that…

I’ve tested it with developer options and the notification show up…

Thank you!

2 Likes

it bites everyone new :wink:

You should be proud that you created a template that actually works without needing to post. That’s a feat on it’s own.

5 Likes

Hallo Gents,

I have the same issue.
How could I test a state via an input_number ?

or

please check why the below codes are not working.
If I unplug a device, no error messages in the log, but no message arrives.
Mail sending is working from powershell and from developer tools as well.

###automations.yaml:
- id: notify_on_device_offline
  alias: "Device offline notification"
  mode: parallel
  trigger:
    - platform: state
      entity_id:
        - fan.air_purifier
        - sensor.smart_air_box_3_temperature
        - sensor.smart_air_box_2_temperature
        - sensor.smart_air_box_1_temperature
        - binary_sensor.zigbee_magnetic_sensor_1
        - binary_sensor.zigbee_magnetic_sensor_2
        - sensor.z_stick_gen5_usb_controller_node_status
      to: "unavailable"
      for:
        minutes: 5
  action:
    - service: notify.mail_xxxsomethingxxx
      data:
        title: "Device offline!"
        message: >
          '{{ trigger.to_state.attributes.friendly_name }} offline more than 5 minutes!'
        target:
          "[email protected]"

###in configuration.yaml
notify:
  - name: "mail_xxxsomethingxxx"
    platform: smtp
    server: "smtp.gmail.com"
    port: 587
    timeout: 35
    sender: "[email protected]"
    encryption: starttls
    username: [email protected]
    password: xxxsomethingpasswordxxx
    recipient:
      - "[email protected]"
    sender_name: "HA"

Did you read the thread? Are you pressing the button in the UI to trigger it? If the answer is yes, that will always error out because there is no trigger object. You have to wait til the trigger fires. See the solution:

Google led me here when I was trying to find a solution to my issue and I did not find one anywhere else, so I would like to share.

I have an automation which is normally triggered by a device state, but sometimes I trigger it with another automation. My first if condition had a trigger.to_state.state which caused an issue when triggering with another automation.

I solved this by adding a condition to check the trigger platform.
Short code:

- if:
    - condition: template
      value_template: "{{trigger.platform == 'state'}}"
    then:
    - if:
      - condition: template
        value_template: "{{trigger.to_state.state == 'on'}}"
      then:

Entire blueprint:

blueprint:
  name: Motion-activated CCL Light
  description: Turn on a light to a color temperature based on time when motion is detected.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    light_target:
      name: Light
      description: Light must be CCT capable
      selector:
        target:
          entity:
            domain: light
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0
          max: 10800
          unit_of_measurement: seconds
    auto_off_time:
      name: Auto Off Time
      description: Time the light will be half lit after the wait time.
      default: 30
      selector:
        number:
          min: 0
          max: 120
          unit_of_measurement: seconds

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input motion_entity

variables:
  lights: !input light_target
  entities: >
    {%- set ns = namespace(ret=[]) %}
    {%- for key in ['device_id', 'area_id', 'entity_id'] %}
      {%- set items = lights.get(key, [])  %}
      {%- if items %}
        {%- set items = [ items ] if items is string else items %}
        {%- set filt = key.split('_') | first %}
        {%- set items = items if filt == 'entity' else items | map(filt ~ '_entities') | sum(start=[]) %}
        {%- set ns.ret = ns.ret + [ items ] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.ret | sum(start=[]) }}
  filtered: "{{ entities | select( 'search' , '^light' ) | select('is_state', 'on') | list }}"
  lights_on: "{{ filtered | count > 0 }}"

action:
  - if:
    - condition: template
      value_template: "{{trigger.platform == 'state'}}"
    then:
    - if:
      - condition: template
        value_template: "{{trigger.to_state.state == 'on'}}"
      then:
      - service: light.turn_on
        data:
          brightness: 255
          color_temp: "{{states('sensor.v_timeofday_mireds')}}"
        target: !input light_target
      else:
        - if: # Check to see if all the lights are off
          - condition: template
            value_template: "{{ lights_on }}"
          then:
            - delay: !input no_motion_wait
            - service: light.turn_on
              data:
                brightness: 127
              target: !input light_target
            - delay: !input auto_off_time
            - service: light.turn_off
              target: !input light_target
    else:
      - service: light.turn_on
        data:
          brightness: 255
          color_temp: "{{states('sensor.v_timeofday_mireds')}}"
        target: !input light_target
3 Likes