I have a sensor that reads whole-house power. I’d like to receive notifications on my phone any time the power changes by 1000W or more in a 2 minute period.
I have set up IOS actions and notifications so I know how to do that, and I believe I need a helper value to store the previous reading to compare, but It looks like I cant use the visual editor to select the helper value to read or write (or at leas I have no clue of the syntax of how to do that.
I believe I need to create the following, but don’t know how:
a) an automation that writes the current power sensor value to the helper value every 2 minutes. I know how to set up the timed automation, but not how to set the value to the current power reading
b) an automation that compares the current power to the previous power
(I think I could do both in the same automation,as long as I do the compare before the update.)
the power meter is sensor.energy_meter_power_meter
Can someone provide working code for such an automation here is my non-function starting point (CAPITALIZED are things I don’t know how to do/specify)?:
alias: Sample Automation
description: ""
trigger:
- platform: time_pattern
minutes: "2"
condition: []
action:
- sequence:
- if:
- type: is_power
condition: device
device_id: dbe91e506afc6990238e726e13f8
entity_id: 1533bc6d39bd35e435c8e82d9cd1
domain: sensor
above: [LAST POWER VALUE+1000]
below: [LAST POWER VALUE+1000]
then:
- service: notify.mobile_app_dwxr
metadata: {}
data:
message: Power changed by [CURRENT POWER - LAST POWER VALUE]
- service: input_number.set_value
metadata: {}
data:
value: CURRENT VALUE OF sensor.energy_meter_power_meter GOES HERE?
target:
entity_id: input_number.last_power_reading
mode: single
I’m sure that is so, so wrong. but here is the pseudo pseudocode version of the flow I think will work:
EVERY 2 MINUTES:
IF (CURRENTPOWER>(LASTPOWER+1000)) OR (CURRENTPOWER<(LASTPOWER-1000)) THEN NOTIFY
LET LASTPOWERHELPERVALUE=CURRENTPOWER
Thanks for helping a brother out! My energy bill this month was $525 and I’m not sure what is driving it, so trying to correlate power consumption with know changes of device states.
There may indeed be a better way. But it does depend on exactly how to interpret your requirement. Does the rate of change need to be high, or do you actually need to compare and see the difference you want with two minutes before? Because that is hard to realize as that situation could differ from second to second. The way you propose might not always see it happen.
The way to get the rate of change is to create a derivative sensor:
If that is above a certain limit, you’d want to trigger an event. If the actual change needs to be bigger, you can add a “for” clause to the trigger, saying the rate of change must have been there for some time. But I’d start with a trigger on the derivative being high.
You could also use a threshold sensor: it combines the derivative and the required limit. But I find it harder to use, because you cannot see the derivative itself, just the outcome.
If you want to continue on the path you chose, let me know, I can help with that too.
Thanks for the suggestion - sure seems easier! I considered derivative initially, but here is why I think it is not really what I’m after:
I’m looking for an absolute change, not a relative one. for example, if I’m at 500W and go to 750W, that is a 50% change, but I don’t really want to see it. But If I’m at 5000W and go to 6000W that is a lower derivative (I believe) but I DO want to see it.
Since I’m specifically looking for step-function changes from one steady state to another steady state, the time factor would not be usable.
even if I use derivative as a trigger, I’s still like to see the actual delta power (current minus previous)
So unless one of the other helpers (trend maybe) would apply, I think a brute force (Power(now) - Power(now-1)) would meet the need best.
Ps, The problem with this is, if wattages fluctuate, you may never see it happen because you sample at 2 min intervals. The drawn (theoretical) example may never fire, while there are definitely moments when power changed by large numbers. The derivative would show the big jumps thouh. A derivative is not a percentage change, but an (absolute) rate of change by the way. More an indication of the angle.
In fact, I used your code and it is working perfectly! My central A/C kicked on and the IOS notification said “Change in power of 5427.6799999999998”. How can I truncate/round it to an integer value?
What you had was just the id of the input helper. That is why the error said it was a string. states() gets the state of that entity for you. States however are always a string, so “| float(0)” converts it to a number, 0 is the default in case of an error (the default is required).
I’ve made lots of progress, but now I’d like to trigger the automation more frequently but then have it abort if the reading from the sensor hasn’t changed. I want to say IF currentpower=previouspower then abort. I tried using condition statement to do that. I know this is wrong, but what is right?:
alias: Power warning
description: Power warning when power deviates by adjustable threshold
trigger:
- platform: time_pattern
seconds: /30
condition:
- condition: numeric_state
entity_id: sensor.energy_meter_power_meter
above: input_number.previous_power
below: input_number.previous_power
If you use both bounds, both need to be true. The value cannot be above and below at the same time. Besides that, it is rather a strange way to say “not equal”. But wattages are seldom exactly equal, it wouldn’t do much anyway. What if the change is 0.1W? or 3? What if it keeps slowly increasing for hours? You ignore them until they add up to 1000?
Actually, I do want to say “Not Exactly Equal”. The reason is that the sensor only provides a new value every 5 minutes, but I want to know as soon as it does, so I’m sampling the sensor every 30 seconds and I want the automation to run as soon as it sees a new value, regardless of how close to the previous one. There is separate logic on when to notify - this is just to log each new sample and the difference.
Is there a way to specify the condition as A<>B? I wasn’t sure if it was an AND or an OR for the over/under.
There is a not, and there is an equals. However, if the sensor only publishes per 5 minutes, why not trigger on the change of the value itself? Then you get the old and the new value in the automation in the trigger variables. That is waaaaaaay easier.
description: "See the change"
mode: single
trigger:
- platform: state
entity_id: sensor.energy_meter_power_meter
not_from:
- unknown
- unavailable
not_to:
- unknown
- unavailable
condition: []
action:
- service: persistent_notification.create
data:
notification_id: power
message: >-
The difference is {{ (trigger.to_state.state | float(0) -
trigger.from_state.state | float(0)) | round(0) }}
This fires the millisecond the change comes in. Now you combine the two to one working one.
But again, if it is in Watts, and you only get one measurement per 5 minutes, then you can blast 3600W for 4.5 minutes and if timed well you won’t see it? I still think this is not the best way to spot high use. Why not use a smart plug to measure a device, and move it to the next if you know the usage?
The devices I’m more interested in are the ones that are least amenable to the smart plug measurement, and I’m also trying to see on an ongoing basis home much we are using AC, pool filter, hot tub heating and pumps, etc.
I’m trying out the trigger you suggested above. I dine trally understand the code but I see in the visual editor it means any change.
Yes, it means any change. I excluded the possibility that the sensor goes offline and becomes unavailable or comes back up from that because then you cannot subtract the value. That was just for good measure, because the error during subtraction would prevent the message anyway.