Noob Question to create custom state

Ok so using HA for a couple of years but I’ve never developed any custom cards etc. and I’m struggling to find a simple guide on how to create a new output based on an input. e.g. I have an existing entity sensor.dish_washer_plug_energy_power and it returns a value in WATTS. I want to create a new card to simply say “The dishwasher is on” or “The dishwasher is off” using the value of the entity >= 5W for ON and < 5W for off. Can someone give me a simple guide pls?

I recently had a similar need to set the state of a binary sensor. After quite a bit of searching around, the only thing that worked for me was a python script…other options may exist but they did not work for me…you can also set the icon with the script.

Put below as file “set_state.py” in ha-home/python_scripts. (re)load it with Menu>Developper Tools > Services > “Python script : reload” and then it is available as a service to be used for e.g. automations.
The yaml of the service call could then look like:

service: python_script.set_state
data_template:
entity_id: binary_sensor.XYZ
state: ‘OFF’

cp/pastof the py-script

#--------------------------------------------------------------------------------------------------

Set the state or other attributes for the entity specified in the Automation Action

#--------------------------------------------------------------------------------------------------

inputEntity = data.get(‘entity_id’)
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()

newState = data.get(‘state’)
if newState is not None:
inputState = newState

newIcon = data.get(‘icon’)
if newIcon is not None:
inputAttributesObject[‘icon’] = newIcon

hass.states.set(inputEntity, inputState, inputAttributesObject)

Here’s how I’d do it:
In your configuration.yaml file:

binary_sensor:
  - platform: template
    sensors:
      dish_washer_stat:
        value_template: "{{ states('sensor.dish_washer_plug_energy_power >= 5.0 }}"
1 Like

Yes thats the kinda thing I was expecting, thank you. However,I set up two in a separate binary_sensors.yaml file like this:

setup

but the state only ever reports unavailable…

… I must be missing something.

Your templates are missing a filter to convert the entity’s state value from a string to a number. They also contains incorrect syntax (missing quotes).

Corrected example:

      value_template: "{{ states('sensor.dish_washer_plug_energy_power') | float(0) > 5 }}"
2 Likes

That has sorted it. Thank you. Thanks all for the prompt advice.

Thanks Taras. I missed that.