Junk reminder automation problem

hi, I’m trying to do my first automation, for the disposal of garbage, but I can’t go on as these errors result, could someone kindly help me, thanks in advance.

the sensors were created with Garbage Collection

2022-11-23 23:35:03.115 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: ‘dict object’ has no attribute ‘from_state’ when rendering ‘Attenzione: domani è giorno di ritiro frazione {{ trigger.from_state.attributes.friendly_name }}.’

2022-11-23 23:35:03.121 ERROR (MainThread) [homeassistant.components.automation.notifica_tts_ritiro_mastello] Notifica ritiro spazzatura: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘from_state’

2022-11-23 23:35:03.137 ERROR (MainThread) [homeassistant.components.automation.notifica_tts_ritiro_mastello] Error while executing automation automation.notifica_tts_ritiro_mastello: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘from_state’

this is the code of my automation:

alias: “Notifica ritiro spazzatura”
trigger:
- platform: numeric_state
entity_id: sensor.plastica
value_template: “{{ state.attributes.days }}”
below: 1
- platform: numeric_state
entity_id: sensor.carta
value_template: “{{ state.attributes.days }}”
below: 1
- platform: numeric_state
entity_id: sensor.umido
value_template: “{{ state.attributes.days }}”
below: 1
- platform: numeric_state
entity_id: sensor.indifferenziata
value_template: “{{ state.attributes.days }}”
below: 1
- platform: numeric_state
entity_id: sensor.vetro
value_template: “{{ state.attributes.days }}”
below: 1
condition: []
action:
- service: notify.mobile_app_iphone
data:
message: “Attenzione: domani è giorno di ritiro frazione {{ trigger.from_state.attributes.friendly_name }}.”

How are you testing the automation?

If you’re testing it by executing the automation via the “Run” command then it will cause the errors you reported.

Reference: Testing your automation

even if I add a time in which the automation must start, it gives me the same errors


alias: Notifica ritiro spazzatura
trigger:

  • platform: time
    at: 08:25:00
  • platform: numeric_state
    entity_id: sensor.plastica
    value_template: ‘{{ state.attributes.days }}’
    below: 1
  • platform: numeric_state
    entity_id: sensor.carta
    value_template: ‘{{ state.attributes.days }}’
    below: 1
  • platform: numeric_state
    entity_id: sensor.umido
    value_template: ‘{{ state.attributes.days }}’
    below: 1
  • platform: numeric_state
    entity_id: sensor.indifferenziata
    value_template: ‘{{ state.attributes.days }}’
    below: 1
  • platform: numeric_state
    entity_id: sensor.vetro
    value_template: ‘{{ state.attributes.days }}’
    below: 1
    condition: []
    action:
  • service: notify.mobile_app_iphone
    data:
    message: ‘Attenzione: domani è giorno di ritiro frazione {{ trigger.from_state.attributes.friendly_name
    }}.’

It will produce the same error because a Time Trigger doesn’t provide trigger.from_state.

Reference: Time

You’re getting the error because you are testing the automation incorrectly.

To test it properly you must wait until one of the Numeric State Triggers gets triggered by the value of its days attribute decreasing below 1.

thank you taras for helping me, i will wait patiently

If you are interested, you can reduce the automation’s size like this:

alias: "Notifica ritiro spazzatura"
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.plastica
      - sensor.carta
      - sensor.umido
      - sensor.indifferenziata
      - sensor.vetro
    attribute: "days"
    below: 1
condition: []
action:
  - service: notify.mobile_app_iphone
    data:
      message: "Attenzione: domani è giorno di ritiro frazione {{ trigger.to_state.name }}."
mode: queued

ok perfect thank you, but wanting to add the notification via alexa? what should i put in the code? I’m sorry if I’m taking advantage but I’m a beginner

and also enter a notification time

That’s easy. For example, this template displays the hour and minute.

{{ now().timestamp() | timestamp_custom('%H:%M') }}

It’s possible but it’s not straightforward. It requires a connection between your Home Assistant and Amazon’s Alexa services and there’s more than one way to do it. You can send a Text-To-Speech message to an Echo via:

  • Home Assistant Cloud (paid service provided by Nabu Casa)
  • Alexa Media Player custom integration
  • Direct connection if your Home Assistant is accessible via the internet

There are other ways as well but the one via Nabu Casa is probably the simplest (but it’s not free).

alexa I can make it talk via “service”, I integrated it into home assistant

Good! Then just use that service.

I created this code, can you tell me if there are errors, or if it can be improved:

alias: Notifica ritiro spazzatura
trigger:

  • platform: numeric_state
    entity_id:
    • sensor.plastica
    • sensor.carta
    • sensor.umido
    • sensor.indifferenziata
    • sensor.vetro
      attribute: days
      below: 1
  • platform: time
    at: ‘20:30:00’
    condition: []
    action:
  • service: notify.mobile_app_iphone
    data:
    title: Domotica
    message: ‘Attenzione: domani è giorno di ritiro frazione {{ trigger.to_state.name
    }}.’
  • service: notify.alexa
    data:
    target: media_player.alexa
    data:
    type: tts
    message: Attenzione: domani è giorno di ritiro frazione {{ trigger.to_state.name }}.
    mode: queued

It will fail to report trigger.to_state.name when it triggers at 20:30 for the reason I explained in my previous post. A Time Trigger only provides trigger.platform and trigger.now.


NOTE

Please format the YAML you post. Unformatted YAML is difficult to read.

Select the YAML and click the </> icon in the forum’s editing menu. For more information, refer to guideline 11 in the FAQ.

and how do I get the notification at a set time?

What do you want the notification to report at 20:30?

the notification both from alexa and on the phone

Based on your reply, you appear to have misunderstood my question.

The Time Trigger doesn’t produce trigger.to_state.name so it cannot use the same message as the one you have when the automation is triggered by its Numeric State Trigger.

The message it uses must reference different information. What is the information you want it to report? For example, do you want it to report the days value of each sensor or only the sensors that are below 1 or what?

I would like to have the notification both on the phone and on alexa of the ‘0’ which cyclically appears on the sensors I created with “Garbage Collection”, at a set time, for example 20:30sensori

This will send notifications only at 20:30 if any sensor’s days attribute is zero.

alias: "Notifica ritiro spazzatura"
variables:
  sensors:
    - sensor.plastica
    - sensor.carta
    - sensor.umido
    - sensor.indifferenziata
    - sensor.vetro
  zero_day: >
     {{ expand(sensors) | selectattr('attributes.days', 'eq' , 0)
       | map(attribute='name') | list }}
trigger:
  - platform: time
    at: '20:30:00'
condition:
  - condition: template
    value_template: "{{ zero_day | count > 0 }}"
action:
  - variables:
      names: "{{ zero_day | join(', ') }}"
  - service: notify.mobile_app_iphone
    data:
      message: "Attenzione: domani è giorno di ritiro frazione {{ names }}."
  - service: notify.alexa
    data:
      message: "Attenzione: domani è giorno di ritiro frazione {{ names }}."
      target: media_player.alexa
      data:
        type: tts

EDIT

Correction. Added missing data: to second notification.