Alert based on the previous value from mqtt

I have script ruuning which is sending a number to the mqtt topic. The mqqt is integrated in home assitant and i am able to receive messages.

My question - Is there a way to trigger an alert in HA if the new value coming to topic is greater than the previous value. For example, i send number 10 every 10 seconds and as soon as it changes to 11 it sends an alert or atleast show a mesaage on the dashboard.

I am pretty new to Home assistant and looking for help on this topic

Yes you can do this with a template condition.

trigger:
  platform: state
  entity_id: sensor.your_mqtt_sensor_here
  to: # null "to:" triggers on any state change but not attribute change
condition:
  condition: template
  value_template: "{{ trigger.to_state.state|float(0) - trigger.from_state.state|flaot(0) > 0 }}"
action:
  service: persistent_notification.create
  data:
    title: "The sensor is increasing"
    message: "Value changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"
1 Like

Thanks for the quick response. Let me give this a try and update you.

I am getting below error -

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘from_state’

yaml -

alias: Monitor Main Door
description: ''
trigger:
  - platform: state
    entity_id: sensor.movement
    attribute: movement_counter
condition:
  - condition: template
    value_template: {{ trigger.to_state.state|float(0) - trigger.from_state.state|float(0) > 0}}
action:
  - service: persistent_notification.create
    data:
      message: Value changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
      title: Door Opened/Closed
mode: single

Below is the sensor data published -

Message 0 received on counter at 2:44 PM:
{
    "movement_counter": 200
}
QoS: 0 - Retain: false

Please format your pasted configuration correctly. See point 11 here:

I have formatted the yaml code.

Ok, that does not work because you changed the trigger to look at an attribute of a sensor, not the sensor state. Thus there is no trigger.to_state, or trigger.from_state.

For this to work it has to trigger on a sensor state.

You could create a template sensor to break out the attribute into its own sensor. Then use that as the trigger.

e.g.

template:
  - sensor:
      - name: "Movement Counter"
        state: "{{ state_attr('sensor.movement', 'movement_counter') }}"

Then use this new sensor, sensor.movement_counter as the trigger entity id.

However there is possibly a simpler method.

What problem are you actually trying to solve as you seem to have a movement sensor with a state you could trigger on?

Thanks for the quick turnaround. I have a ruuvitag and my plan is to put this tag on a door and monitor when it is opened. The final plan is that when I leave the home I will arm the automation (add another condition to check for a flag) and if any one tries to open the door it alerts me.

I live in an apartment and dont want to invest money in buying a door/windows sensor right now.

I am very new to Home Assistant still trying to learn the basics.

Ok, then the template sensor is the way to do this.

It worked. Thanks a lot for all the help.

1 Like