Automation: Create toggle based on entity reporting watts

Is this possible? I have an energy meter that reports the watts being drawn from an outlet. I want a switch/toggle that reports (not controls) whether the watts are >0 or 0. >0 = on, 0 = off. Is this possible, and if so, how?

The typical way to do this is to create a Template Sensor. If you can provide the details of the entity that contains the power being used (i.e., entity_id, state, attributes), I could make a concrete suggestion.

Ah I haven’t gotten into templates yet - this sounds helpful.

I’m working with this: sensor.sump_pump_power

It outputs an int (I believe a true int, not a string). It can be any number from 0 to 13 I believe.

The state of every entity is a string. Period. End of story. :slight_smile:

binary_sensor:
- platform: template
  sensors:
    sump_pump:
      value_template: "{{ states('sensor.sump_pump_power')|int > 0 }}"

If the numeric value of the sensor’s state is actually a float, then change “|int” to “|float”, otherwise, e.g., 0.1 would get converted to 0 and hence result in off instead of on.

Ah cool, thanks.

Where is this supposed to go though? Again, I’d like to use it in an automation. Do I put it directly there or somewhere else to be called upon by the automation?

You had said:

So I thought you were looking for an entity you could show in the front end that indicated whether or not the power was zero. Hence my suggestion.

If you want to trigger an automation based on whether or not the power is zero, you could do one of two things. Either you use the template binary sensor:

trigger:
- platform: state
  entity_id: binary_sensor.sump_pump
  to: 'on'

Or you use the power sensor directly (and skip creating the template binary sensor):

trigger:
- platform: numeric_state
  entity_id: sensor.sump_pump_power
  above: 0

Ah yeah sorry that was poorly asked.

Thanks! Your second option is what I was doing, but it has some issues (like if HA restarts) so I was looking for logic to detect the change rather than the value itself. Looks like your first option does just that! I’ll give it a shot.

Thanks again.

Edit: wait, does binary_sensor always exist? I guess this was my issue - I don’t have a binary_sensor version of this sensor.

Edit 2: Figured it out I think! Added the following to my configuration.yaml

binary_sensor:
  - platform: template
    sensors:
      sump_pump:
        friendly_name: "Sump Pump Status"
        value_template: >-
          {{ state_attr('sensor.sump_pump_power', 'power')|float > 0 }}

Which gave me access to this in my automation trigger:

entity_id: binary_sensor.sump_pump
for: '00:01:00'
from: 'off'
platform: state
to: 'on'

There would be absolutely no difference between the two. They both require sensor.sump_pump_power to change before they will “do” anything.

It will exist when you add it to your configuration.

There you go. But that actually tells me that what you told me before was inaccurate. You said the state of the sensor was the power, but now you’re showing it as an attribute of the sensor. Those are two different things. FWIW, as an attribute, it’s very possible that it is a number already (as opposed to a string), so you may not actually need the float filter.

Always learning.

But the difference between the two is that when it’s binary, I can write an automation to trigger when it changes from on to off. When it’s a float, I can’t (as far as I can tell).

I showed you above how to do it both ways. They are exactly equivalent. The template binary sensor directly follows the changes in sensor.sump_pump_power.

Can we take a step back? What are you ultimately trying to do? You say you want to trigger an automation, but what does that automation do?

Yep no more help needed, it works.

It triggers a text to be sent to me when the sump pump turns on. The issue with the other way is that when HA restarts it triggers. Detecting a change from no to yes or yes to know resolved that issue.

Thanks again, all set.