Getting the parent of an attribute to get another attribute

So I’m currently doing this, which I don’t like as it hard codes the entity instead of relying on the ‘entity’ element.

  - entity: sensor.christine_battery_level
    card_mod:
      style: |
        :host {
            {% set level = states(config.entity) | int %}
            {% set charging = is_state("sensor.christine_charging", "charging") %}

I tried {{ state_attr('sensor.christine_battery_level', 'charging') }} in the Developer Template but it returns “none” while it should be returning “charging”.

So, what’s the proper way to get this ‘charging’ attribute knowing the attribute ‘sensor.christine_battery_level’?

PS: It’s inside a picture-elements card.

Thanks.

Isn’t these separate sensors? They are not linked to each other as you believe.
The only way they are linked is with the device or naming convention.

Using the naming convention you probably could do something like:

- entity: sensor.christine_battery_level
    card_mod:
      style: |
        :host {
            {% set level = states(config.entity) | int %}
            {% set charging = is_state(config.entity.split("_")[0] ~ "_charging", "charging") %}
1 Like

Try:

{% set charging = is_state(config.entity.split(‘_battery_level’)[0]+’_charging’, ‘charging’) %}
1 Like

Yeah, they all show under the device’s object.

I did this but I think it’s an overkill. Yours is much simpler as it doesn’t have to iterates through all the device’s attributes to find the good one.

{% set charging = is_state((device_entities(device_id(config.entity)) | select("search", "charging") | list)[0], "charging") %}