Convert State sensor to Binary sensor

I’m passing the battery state over to HA via MQTT. One of the attributes is the charging status, and to grab this I’m using the following without issues:

- platform: template
  sensors:
    ls_charge_tablet:
      friendly_name: "Tablet Charge State"
      value_template: >-
        {{ states.sensor.tablet_lstablet_ls_battery_sensor.attributes.charging_state }}

This creates a sensor that returns a value of charging / discharging. However, I’d prefer to convert this to a binary sensor if at all possible. This would need to return on/true when the state is ‘charging’ and false/off at other times.

I can’t work out how to do this despite looking at HA docs for binary template sensors. Any assistance would be greatly appreciated.

binary_sensor:

- platform: template
  sensors:
    ls_charge_tablet:
      friendly_name: "Tablet Charge State"
      value_template: "{{ state_attr('sensor.tablet_lstablet_ls_battery_sensor', 'charging_state') == 'charging' }}"
1 Like

Thanks, that works perfectly.

1 Like

I’m trying to do a similar thing with the Android app but getting no data with the following template. Stole the config from my opengarage yaml.

    nokia_charging_state:
      friendly_name: Nokia Charging State
      value_template: '{% if states.sensor.nokia_battery_level %}
            {% if states.sensor.nokia_battery_level.attributes["is_charging"] == "false" %}
              No
            {% elif states.sensor.nokia_battery_level.attributes["is_charging"] == "true" %}
              Yes
            {% endif %}
            {% endif %}'
      icon_template: >
          '{%- if is_state('sensor.nokia_charging_state', 'Yes') -%}mdi:battery-charging-high{% else %}mdi:battery-medium{%- endif -%}'

Can you spot what I’m doing wrong? Sensor reports as true or false in states panel.

FYI, I’ve also tried this which reports false even though it should be true.

{% if is_state('states.sensor.nokia_battery_level.attributes.charger_type', 'AC') %} True
{% else %} False
{% endif %}

It’s generally considered bad form to resurrect and old forum thread with a new question. You’ll have much more chance of getting help with your issue if you open a new thread.

Thanks. I’ll make sure I do that in the morning.

Good advice for next time.

While I’m here I might as well show what I think should work, and what I think you were trying to do:

    nokia_charging_state:
      friendly_name: Nokia Charging State
      entity_id: sensor.nokia_battery_level
      value_template: >
        {% if not states('sensor.nokia_battery_level') == 'undefined' %}
          {% if state_attr('sensor.nokia_battery_level', 'is_charging') %}
            Yes
          {% else %}
            No
          {% endif %}
        {% else %}
          Undefined
        {% endif %}
      icon_template: >
        {% if is_state('sensor.nokia_charging_state', 'Yes') %}
          mdi:battery-charging-high
        {% else %}
          mdi:battery-medium
        {% endif %}

Hey thanks for that. No wonder I couldn’t figure it out!! Testing now. Appreciate your input as always.