Help needed to compare trigger.from_state and trigger.to_state for automation activations filtering

Hi. Im’ trying to get rid of frequent notifications from an automation that should only notify me when my public ip changes, but runs every time the public ip sensor is updated without any change.
Here the code of my automation:

alias: '[WAN] [01] - Nuovo IP Pubblico'
description: Avvisa al cambio di IP Pubblico
trigger:
  - platform: state
    entity_id:
      - sensor.ip_pubblico
condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - service: notify.mobile_app_iphone_3
    data:
      message: '{{ states.sensor.ip_pubblico.state }}'
      title: Il Tuo IP Pubblico è cambiato in
mode: single

The error message I’m currently receiving is "template value should be a string for dictionary value @ data[‘value_template’]. Got None".
What am I doing wrong?
Thanks!!

Shouldn’t it be like this ?

    value_template: '{{ trigger.from_state != trigger.to_state }}'

I’ve got the same error message "template value should be a string for dictionary value @ data[‘value_template’]. Got None"
In sensor.yaml this is my public ip sensor:

- platform: rest
  name: IP Pubblico
  resource: http://ip.jsontest.com
  value_template: "{{ value_json.ip }}"

I’ve tried to access its value with

{{ state_attr('sensor.ip_pubblico', 'from_state')}}

but i get “None”

That is a known issue with the automation editor:

Your template condition is fine.

Your message should be this though:

message: "{{ states('sensor.ip_pubblico') }}"

https://www.home-assistant.io/docs/configuration/templating/#states

Thank you!

The working code is now:

alias: '[WAN] [01] - Nuovo IP Pubblico'
description: Avvisa al cambio di IP Pubblico
trigger:
  - platform: state
    entity_id:
      - sensor.ip_pubblico
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: template
    value_template: '{{ trigger.from_state != trigger.to_state }}'
action:
  - service: notify.mobile_app_iphone_3
    data:
      message: '{{states('sensor.ip_pubblico')}}'
      title: Il Tuo IP Pubblico è cambiato in
mode: single

Since it happened that I have received some duplicated notifications, finally I switched to a sensor made in Node-Red and the node node-red-contrib-public-ip-address.

Now the automation is really simplified without conditions, since the Node-Red flow doesn’t output state changes if the public ip address doesn’t change.

Here a screenshot of that simple flow and the code for sharing :slight_smile: Thanks to you all!

Flow code:

[{"id":"c0facedc.0275a","type":"tab","label":"Sensore Ip Pubblico Per HA","disabled":false,"info":""},{"id":"ea8e2a74.4bfd98","type":"public-ip-address","z":"c0facedc.0275a","name":"","x":410,"y":300,"wires":[["5a6babd4.99f614"]]},{"id":"57c36e65.78a9d","type":"inject","z":"c0facedc.0275a","name":"Trigger ogni minuto","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":300,"wires":[["ea8e2a74.4bfd98"]]},{"id":"5a6babd4.99f614","type":"ha-entity","z":"c0facedc.0275a","name":"Ip Pubblico Node-Red","server":"b6476084.b9987","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Ip Pubblico Node-Red"},{"property":"device_class","value":"sensor"},{"property":"icon","value":"mdi:ip-network"},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload","stateType":"msg","attributes":[{"property":"entity_id","value":"sensor.node-red.ip_pubblico","valueType":"str"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"","outputPayloadType":"str","x":620,"y":360,"wires":[["9deef2f7.9c2e1"]]},{"id":"4794ed24.5bc924","type":"inject","z":"c0facedc.0275a","name":"Test Ip 37.128.5.123","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"37.128.5.123","payloadType":"str","x":210,"y":360,"wires":[["5a6babd4.99f614"]]},{"id":"3a43bebf.8bfa32","type":"inject","z":"c0facedc.0275a","name":"Test Ip 99.99.99.9","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"99.99.99.9","payloadType":"str","x":210,"y":420,"wires":[["5a6babd4.99f614"]]},{"id":"9deef2f7.9c2e1","type":"debug","z":"c0facedc.0275a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":360,"wires":[]},{"id":"b6476084.b9987","type":"server","name":"HA-Local","version":2,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Because you changed your condition from this (which is correct):

condition:
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'

To this (which is not)

condition:
  - condition: template
    value_template: '{{ trigger.from_state != trigger.to_state }}'

You want to compare the from and to states. Not the to and from state objects.

Hi guys!
This looks like a suitable thread for my question.
I have fiddled now with it a fair share of time and incline to think I’m into some strange bug here. Or I’m just plain stupid. Latter is more probable tho.

Anyway, I have a super simple automation to let know when car is locked-unlocked. Great and works!
Now what I want to exclude is when BMW Cloud connectivity is lost and I wouldn’t get false alarms once it’s back up, so I added conditions to make sure we are not dealing with unavailable values.

So what I do have is this:

alias: Notify on BMW Lock events
description: ""
trigger:
  - platform: state
    entity_id:
      - lock.320d_xdrive_lock
condition:
  - condition: template
    value_template: trigger.to_state.state != 'unavailable'
  - condition: template
    value_template: trigger.from_state.state != 'unavailable'
action:
  - service: notify.mobile_app_tarmos_iphone
    data:
      message: >-
        BMW now {{states('lock.320d_xdrive_lock')}}, from
        {{trigger.from_state.state}} to {{trigger.to_state.state}}
      title: BMW lock
mode: single

All works there without the conditions block. Without it, I get nice updates like:

The issue is - and I can’t figure it out - no matter of the state values I play with in the Dev Tools - with the conditions block as above - I get nothing and in trace it falls to the first branch:
image
… with

Result:
result: false
entities:

Essentially, I cant get trigger.to | from_state.state expressions to work, but displaying wise - all OK.

Thoughts? What I do wrong that I can’t spot?

Probably should be something like

    value_template: '{{trigger.to_state.state != "unavailable"}}'

In other words, get your quoting more like the examples earlier in the thread.