Help me to catch the state of one sensor entity

Hi,

I’m quite new with HA, but I’m struggling to get the state of one custom component and create a sensor with it to create some automation with it.

In that case I want to use “Desired:” value

note that there isn’t any separation between values

image

I’ve tried something like that without results

sensor:
  - platform: template
    sensors:
      ac_degrees:
        friendly_name: "AC degrees"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sensor.info_room', 'Desired') }}"

The trouble is the junk after the number in your attribute. Try:

sensor:
  - platform: template
    sensors:
      ac_degrees:
        friendly_name: "AC degrees"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('sensor.info_room', 'Desired').split('Â')[0]|float }}"

.split('Â')[0] splits the string into a list of sub-strings at the occurrence of  and returns the 1st item from the list (list numbering starts at 0).

|float converts the string to a number, you can use |int instead, if there are no decimal places. Only required if you want to manipulate the number in the template (scale it by x2 for example) and could be left out in this case where you are not performing mathematical operations.

Edit: Just noticed this:

I assumed it was a separate attribute. It is not. So my solution won’t work. Listen to 123 below…

Something is wrong with that sensor. You don’t have attributes, your entire state contains the attributes. How did you create this sensor?

It’s a custom component. I would change its code so that it produces attributes and not just a state value containing a jumble of data.

2 Likes

Hah, nothing to correct! Something is up with that custom component.

1 Like

I just edited that out too.

It might be using a newline to delimit each key-value pair … but probably not.

You can use regex_findall_index to extract the temperature. However, that’s a bandaid solution. The best would be to fix the custom component.


EDIT

This might work. I can’t tell for sure if there’s a space between Desired: and 21 so you may have to adjust the template’s regex pattern.

sensor:
  - platform: template
    sensors:
      ac_degrees:
        friendly_name: "AC degrees"
        unit_of_measurement: 'degrees'
        value_template: "{{ states('sensor.info_room') | regex_findall_index(find='Desired: (\d+\.?\d+)', index=0, ignorecase=False) }}"

Sorry guys,

It doesn’t work. with the regex I get a fail, checking the config and with the first solution I get an unknown state as a result.

Finally I’ve modified the code of this sensor, to get the number directly.

Thanks for your help :slight_smile: