State triggered automations don't execute

Hello.

After upgrading from 0.103.5 to 0.107.5 , all my state triggered automations do not execute anymore.

For instance, the simple code below worked fine before upgrading, but now nothing happens when the state changes.

  - alias: "Belysning: Lamporna i skafferiet"
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.skafferidorren
    action:
      - service: notify.ilias_galaxy_note_8
        data:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ state('binary_sensor.skafferidorren') }}"

When the state of the binary_sensor i changed, a notification is sent to my mobile telling me the state of the binary_sensor.

I have made sure that:

  • The state of the binary sensor does change.
  • The notification does work fine in all my other automations.
  • The code validates correctly.
  • I have checked all the breaking changes from 0.103 to 0.107 without being able to find anything that explains this odd situation.

Any idea of what might have gone wrong is really appreciated!

try using data_template: ?

that is, unless your device isn’t recognized anymore. can you see it in the notify service of developer-tools?

2 Likes

yep, data_template would be your problem.

Also you ahve a typo in your template, missing ‘s’ on state
 states

          message: "Skafferidörren: {{ states('binary_sensor.skafferidorren') }}"

Hello @Mariusthvdb and thanks for your reply.

I tried this:

- alias: "Belysning: Lamporna i skafferiet"
    initial_state: true
    trigger: 
      platform: template
      value_template: "{{ state('binary_sensor.skafferidorren') }}"
    action:
      - service: notify.ilias_galaxy_note_8
        data:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ states('binary_sensor.skafferidorren') }}"

and this:

- alias: "Belysning: Lamporna i skafferiet"
    initial_state: true
    trigger: 
      platform: template
      value_template: "{{ is_state('binary_sensor.skafferidorren','on') }}"
    action:
      - service: notify.ilias_galaxy_note_8
        data:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ state('binary_sensor.skafferidorren') }}"

I also tried using events:

- alias: "Belysning: Lamporna i skafferiet"
    initial_state: true
    trigger: 
      platform: event
      event_type: state_changed
      event_data:
        entity_id: sensor.fibaro_system_fgdw002_skafferidorren_opening_sensor_2_access_control
    action:
      - service: notify.ilias_galaxy_note_8
        data:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ state('binary_sensor.skafferidorren') }} "

Nothing happens.

I can se the device in the states tab and I can also trigger the automation from the developers tab. But I receive no notification.

All other trigger, such as time do work fine.

Is there some setting I might have turned of by accident?

No, you changed the wrong template.
I kinda find it hard to believe this ever worked as you posted it, so I don’t think this was an upgrade issue.

Third line from the bottom. Use data_template not data.

  - alias: "Belysning: Lamporna i skafferiet"
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.skafferidorren
    action:
      - service: notify.ilias_galaxy_note_8
        data_template:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ states('binary_sensor.skafferidorren') }}"

Using data instead of data_template???
Unlikely.


EDIT

Petro identified the following error: original template contains a typo. Should be states function not state. See petro’s post below containing streamlined version.

well, that could be possible indeed,as the notify service appears no to need the data_template field to be able to use templates


still, Ive always found that an odd thing, so use data_template if a template is needed.

sounds like something else is going on.

@ibennani : can you show us the automation in the developer-tools/state, so we can se it actually is ‘on’ ?

I know you’ve used ‘initial_state: true’ but maybe you have restarted HA, and the automation got turned_off somehow? btw, you can leave out the initial_state, since states of automations are restored at startup. Unless of course you happen to switch these automations on/off during operation, and you want to make sure it is on at startup.

Also, concentrating on this automation, are you sure the binary_sensor has changed state? If not, it wouldn’t trigger


Well he is triggering on a change to either state so he’s got twice the chance, but your right, it it don’t change it don’t trigger. but he does say : -

As for a message notification template not needing data_template - That’s just wrong in all sorts of ways "It’s Agin te lawz ‘o’ natur " (btw, I’m not blaming you for that ! )
:rofl:

simply check the only example here: Group - Home Assistant 

need I say more

since he can trigger manually, and the message is notified, we can conclude it has to do with either the trigger, or the condition block, not the action block.

a- automation is off
b- trigger isn’t changing
c- condition isn’t evaluated to true

d- syntax is incorrect (which not always throws an error)


Sorry my last edit was just before yours, I believe it’s ‘correct’ it’s just ‘wrong’ if you get my drift

And I understand (and would do the same) your use of data_template with any data requiring template.

haha, yes, no worries. I didn’t take it personally. Not that kind of HA addict.

for @ibennani, this is working perfectly here:

- alias: 'Notify Hub Offline'
  id: 'Notify Hub Offline'
#  initial_state: 'on'
  trigger:
#    platform: template
#    value_template: >
#     {{ expand('group.hubs_binary_pinged')|map(attribute='entity_id')| list | join(', ')}}
    platform: state
    entity_id:
      - binary_sensor.asus_router
      - binary_sensor.hassio_mqtt
      - binary_sensor.hassio_rpi4
      - binary_sensor.ikea_tradfri
      - binary_sensor.philips_hue
      - binary_sensor.iungo
      - binary_sensor.solaredge
      - binary_sensor.timecapsule
      - binary_sensor.synology
      - binary_sensor.kantoor_thermostat
    to: 'off'
    for:
      seconds: 30
  condition: []
#    condition: template
#    value_template: >
#     {{ state_attr('automation.notify_hub_offline','last_triggered') != none and
#        as_timestamp(now()) | int -
#              as_timestamp(state_attr('automation.notify_hub_offline','last_triggered'))
#             | default(0) | int > 30 }}
#no extra notification condition, always send notification!
  action:
#    - service: homeassistant.update_entity
#      entity_id: binary_sensor.hubs_offline
    - service: notify.notify #HA app notification
      data_template:
        title: 'Hassio Rpi4 system message: Hub offline!'
        message: >
          {{as_timestamp(now())|timestamp_custom('%X')}} :
          {{trigger.to_state.attributes.friendly_name}} has gone offline.
          Check status to restore functionality.'
    - service: notify.system #Pushbullet notification
      data_template:
        title: 'Hassio Rpi4 system message: Hub offline!'
        message: >
          {{as_timestamp(now())|timestamp_custom('%X')}} :
          {{trigger.to_state.attributes.friendly_name}} has gone offline.
          Check status to restore functionality.'
    - service: persistent_notification.create
      data_template:
        title: 'Hassio Rpi4 system message: {{trigger.to_state.name}} offline!'
        message: >
          {{as_timestamp(now())|timestamp_custom('%X')}} :
          {{trigger.to_state.name}} has gone offline. Check status to restore functionality.
        notification_id: >
          {{trigger.to_state.object_id}}-hub-notification
    - service: script.intercom_message
      data_template:
        message_en: >
         Hassio Rpi4 system message: Hub {{trigger.to_state.name}} has gone offline. Check status to
         restore functionality.
        message_nl: >
          Hassio Rpi4 systeem bericht: Hub {{trigger.to_state.name}} is uitgeschakeld.
          Controleer status om functionaliteit te herstellen.

and its got all the components you use. Only thing I can see is you are not using a list. Give that a try?
Also, use {{states('binary_sensor.skafferidorren')}} not {{state('binary_sensor.skafferidorren')}} (mind the s
) which is what you missed in the template to begin with, so that wouldn’t be correct


(or use trigger.to_state.state )

- alias: "Belysning: Lamporna i skafferiet"
   initial_state: true
   trigger:
     platform: state
     entity_id:
       - binary_sensor.skafferidorren
   action:
     service: notify.ilias_galaxy_note_8
     data_template:
       title: >
         HEMMET {{now().strftime('%R') }}
       message: >
         Skafferidörren: {{states('binary_sensor.skafferidorren')}}
         Skafferidörren: {{trigger.to_state.state}}

There’s no way this worked before because of his incorrect template alone:

should be states

1 Like

This will work.

  - alias: "Belysning: Lamporna i skafferiet"
    trigger:
      platform: state
      entity_id: binary_sensor.skafferidorren
    action:
      - service: notify.ilias_galaxy_note_8
        data_template:
          title: "HEMMET {{ now().strftime('%R') }}"
          message: "Skafferidörren: {{ trigger.to_state.state }}"
1 Like

Hello.
This is one of those embarrassing moments when you post something, going away for a few hours and when you come back, the thread is flooded with comments telling you’re wrong.


and all the comments are true!!! :scream:

Of course that row of code did not work. I have been around for to long to even say it dit work


Anyway, thanks a lot everyone for your commitment in my question. @petro, your solution did work as expected. THANKS!!!

1 Like