[question] How to change a number (value) into a word?

Hi,

I am looking for a solution and have not found anything yet.
I am specifically concerned about this:
I read out a status, e.g. battery charging, via Modbus, but unfortunately I only get one number.
Let’s say: Battery charging = 15, Battery discharging = 10…whatever.

Is there a way to convert the numbers into a word, that I get a word for the dashboard instead of a number?

Template Sensors

Add this to a template sensor (converted from some python found online maximum 99)

- name: "EPEVER Charging Status in Words"
  state: >-
    {% set number = states('sensor.epever_charging_status') | float(0) | round(0) %}
    
    {% set number_list = ["zero","one","two","three","four","five","six","seven","eight","nine"] %}
    {% set teen_list = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"] %}
    {% set decades_list =["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"] %}
    
    {% if number <= 9 %}
        {{ number_list[number].title() }}
    {% elif number >= 10 and number <= 19 %}
        {% set tens = number % 10 %}
        {{ teen_list[tens].title() }}
    {% elif number > 19 and number <= 99 %}
        {% set ones = (number / 10) | int() %}
        {% set twos = ones - 2 %}
        {% set tens = number % 10 %}
        {% if tens == 0 %}
            {{ decades_list[twos].title() }}
        {% elif tens != 0 %}
            {{ decades_list[twos].title() + " " + number_list[tens] }}
        {% endif %}
    {% endif %}

Actually I think this is what you mean, just repeat the ELIF lines for each value

- name: "EPEVER Charging Status in Words"
  state: >-
    {% set state = states('sensor.epever_charging_status') %}
    {% if state == 15 %}
      Battery Charging
    {% elif state == 10 %}
      Battery Discharging
    {% else %}
      Unknown
    {% endif %}

I think you have a set of bits that represent different settings and HA is just showing them as a decimal number.
10 would then mean bit 2 and 4 is set, so you actually have two settings active in that number.

I replied to another post in regards to bitmasking here: Help with bitmasking a modbus address

1 Like

Hi @WallyR,

Thanks for your reply.
Your feedback might help in my case, because the adresses look like that:

        hub: epever
        slave: 1
        register: 12801
        register_type: input
        scale: 1
        precision: 0
        #######################################################################
        #D15-D14: Input voltage status. 00H normal, 01H No input power connected, 02H Higher input voltage , 03H Input voltage error.
        #D13: Charging MOSFET is short circuit.
        #D12: Charging or Anti-reverse MOSFET is open circuit.
        #D11: Anti-reverse MOSFET is short circuit.
        #D10: Input is over current.
        #D9: The load is over current.
        #D8: The load is short circuit.
        #D7: Load MOSFET is short circuit.
        #D6:Disequilibrium in three circuits.A17
        #D4: PV input is short circuit.
        #D3-D2: Charging status. 00H No charging,01H Float,02H Boost, 03H Equalization.
        #D1: 0 Normal, 1 Fault.
        #D0: 1 Running, 0 Standby.
        #Status analysis
        #Array status:address 3201 bits D15-D10
        #Charging status:address 3201 bits D3-D2
        #Battery status: address 3200 bits D7-D0
        #Load status: address 3201 bits D9-D7,
        #Device status: address 3200 bit D15 address 3202 bits D13-D8,D6-D4 address 3201 bits D6 address 2000

Hi,

Thanks for your reply.
I didn´t get that running in my case.
I keep you updated. :wink: