Need help with trigger.to_state != x

Hi everybody,

I am trying to get a TG notification when anything but x is the state of a particular sensor. Sensor code:

sensor:
  - platform: template
    sensors:
      fritzbox_callmonitor:
        friendly_name: "Fritzbox Anrufe"
        value_template: >
          {% if is_state('sensor.fritz_box_7490_call_monitor_telefonbuch', 'idle') %}
            Keine Telefonaktivität
          {% elif is_state('sensor.fritz_box_7490_call_monitor_telefonbuch', 'dialing') %}
            Rufe {{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'to_name') }} ({{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'to') }}) an
          {% elif is_state('sensor.fritz_box_7490_call_monitor_telefonbuch', 'ringing') %}
            Eingehender Anruf von {{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'from_name') }} ({{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'from') }})
          {% else %}
            Spreche mit {{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'with_name') }} ({{ state_attr('sensor.fritz_box_7490_call_monitor_telefonbuch', 'with') }})
          {% endif %}
        icon_template: "fas:blender-phone"

I tested this sensor multiple times. The state is usually Keine Telefonaktivität, which just means idle. I took the example from the official docs.

In the docs, they created an automation instead of a template sensor; however, I’d like to always display the current state in my VIP lovelace dashboard as well, so I made it into a template sensor. My automation code:

automation:
  - alias: "Fritzbox Call Monitor"
    trigger:
      - platform: state
        entity_id: sensor.fritzbox_callmonitor
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: input_boolean.helper_notifications_vip
            state: "on"
          - condition: template
            value_template: >-
              {{ (trigger.to_state != trigger.from_state) and (trigger.to_state != "Keine Telefonaktivität")}}
    action:
      - service: notify.tg_me
        data:
          message: >
            ☎️ Fritzbox meldet {{ states("sensor.fritzbox_callmonitor") }} ({{ now().strftime("%H:%M:%S)}})

The automation seemed to work fine when my value_template was only {{ trigger.to_state != trigger.from_state) }}. However, I don’t want to receive a notification each time a call (or even just ringing the phone) ends, so I added the (trigger.to_state != "Keine Telefonaktivität") part.

What is wrong about that line? I called my SIP phone from my mobile phone. The second it began to ring, I received Fritzbox meldet Eingehender Anruf von unknown (number) (time). Right after, when I hung up, I received Fritzbox meldet Keine Telefonaktivität (time). The message template works fine, but I don’t want to receive that second notification at all.

Now, I could remove that part about idle from the template sensor all together. But since I want to display it in lovelace, I would prefer to keep it. What do I need to change in my automation so that it will work? I thought of changing the second trigger.to_state part to sensor.fritzbox_callmonitor != "Keine Telefonaktivität" (so using the actual sensor instead of the trigger.to_state “placeholder”… but I try to make things as interchangeable as possible and avoid hard coding entities as much as possible.

Thank you in advance for your help :slight_smile:

Change

trigger.to_state

To

trigger.to_state.state

The first one is the whole state object, including the state, name, etc., but you only want to check the new state.

3 Likes

Oh, of course!! Thank you so much :slight_smile: