Friendly states

I am trying to get a message in Telegram with the current status of a group.

import appdaemon.plugins.hass.hassapi as hass
import datetime

class test(hass.Hass):

  def initialize(self):

   self.listen_state(self.test, "input_boolean.dvd_mode")


       
  def test (self, entity, attribute, old, new, kwargs):
       group_skal = self.get_state("group.skal", attribute="entity_id")
       #friendly_name = self.get_state(entity, attribute="friendly_name")
       a = []
       for entity in group_skal:
           friendly_name = self.get_state(entity, attribute="friendly_name")
           a.append(friendly_name+ ' er ' + self.get_state(entity))
       self.log( '\n'.join(map(str, a)))
       self.notify( '\n'.join(map(str, a)), name = "telegram_morten")

When I trigger the “input_boolean.dvd_mode” (just my test trigger) I get a message:
“Main door is off
Kitchen window is on
etc.”

I would like to get the same states as in Home Assistant, meaning
“Main door is closed
Kitchen window is open
etc”

In order words I need a “friendly_states”, but what is that called?

I could map on and off to open and closed, but that wouldn’t work using the same app with temperatures etc.

These “friendly states” as you call them don’t exist as far as I know. Home Assistant just displays the status differently based on the device class of the binary sensor, but the state of the entity will always be “on” or “off”.

You should still be able to include this in your code if I understood you correctly. If “on” always means “open” and “off” always means close, you can just have some “if state is equal to off then text is closed, else if it is equal to on the text is open, else the text is the state”
Otherwise you could try to incorporate the device class and then decide what the text should be based on the device class.

1 Like

I was afraid that was the answer, but then I just work around it by if statements, but it would have been nice though :slight_smile:

I think this would be a really useful as a helper method.

A defined dict like the following could handle it:

friendly_state = {
  "door": {
    "on": "open",
    "off": "closed",
  },
  "motion": {
    "on": "detected",
    "off": "clear",
  },
}

And so on.

A method like this could do the work:

def get_friendly_state(self, entity_id):
  state = self.get_state(entity_id, attribute="all")
  if state is None:
    return None
  try:
    device_class = state.get('attributes', {}).get('device_class', 'unknown')
    friendly_state = friendly_states.get(device_class, {}).get(state['state'], state['state'])
  except:
    friendly_state = state['state']
  return friendly_state

Probably needs some work as it’s untested. But I’d love to see a PR for this if anyone is up to the task.

2 Likes

You put my thoughts into words (code) :slight_smile: That’s how I would have done it and would love to do the PR but unfortunately I’m too busy at the moment :frowning:

I totally understand. My time is limited at the moment as well. :frowning:

Someone will get to it eventually. Until then, all of my sensor notifications still read like “Back Door is off”. lol

For telegram, you can add logic to check and transform the state, like below:

message: >
            {% if (states.switch.garage_door_1.state == "off") %}Garage 1 is closed. {% else %}Garage 1 is open. {% endif %}        
            {% if (states.switch.garage_door_2.state == "off") %}Garage 2 is closed. {% else %}Garage 2 is open. {% endif %}    

This comes straight out of my config and works.

Like you, I would an easier way to alias states so they would be portable across interfaces. Telegram is working for me, but the state on the homehabit app is still confusing.

Know this is old, but you can look at the source. Example for weather: