Mqtt sensor with attributes of mixed data types

I have an MQTT sensor as follows:

 78   . - name: "Home Electricty"                                
 79   . . object_id: "electric_panel"                                
 81   . . state_topic: "electric_panel/datatest"                                
 84   . . value_template: "{{ value_json.status }}"                                                                                                                                                                                                         
 85   . . json_attributes_topic: "electric_panel/datatest"                                
 86   . . json_attributes_template: >                                
 87   . . . {                                
 88   . . . . "voltage1":  {{ value_json.voltage1 }},                                
 91   . . . . "current1":  {{ value_json.current1 }},                                
 94   . . . }                                

This works for the most part, but there’s something a little weird to me.

If I make my mqtt message that’s carrying my json data come in, and the “status” in my json object is { “status”: 1}. All my other json values are numbers, and it works fine.

But if I change my json to { “status”:“on”}. Then nothing works. It doesn’t pick up on any of the attributes. Why does it do this? And I can’t figure out how to fix it.

Do you have a unit_of_measurement defined as well or is that the complete config?

Only sensors with numeric values can have a unit of measurement defined.

Also in future please don’t include line numbers and dots as spacers. It would make it very difficult to copy, paste and edit your config to show you a correct answer. The code blocks you have used to correctly format your pasted config will preserve the indentation.

Good call on line numbers and dots. Won’t do that again.

No, I don’t have a unit_of_measurement defined. Here’s me entire code block:

    sensor:                                
      - name: "Home Electricty"                                
        object_id: "electric_panel"                                
        unique_id: "f2536c24"                                
        state_topic: "electric_panel/data"                                
        suggested_display_precision: 1                                
        value_template: "{{ value_json.status }}"                                
        json_attributes_topic: "electric_panel/data"                                
        json_attributes_template: >                                
          {                                
            "voltage1":       {{ value_json.voltage1 }},                                
            "voltage2":       {{ value_json.voltage2 }},                                
            "voltage":        {{ value_json.voltage }},                                
            "current1":       {{ value_json.current1 }},                                
            "current2":       {{ value_json.current2 }},                                
            "current":        {{ value_json.current }},                                
            "power1":         {{ value_json.power1 }},                                
            "power2":         {{ value_json.power2 }},                                
            "power":          {{ value_json.power }},                                
            "energy1":        {{ value_json.energy1 }},                                
            "energy2":        {{ value_json.energy2 }},                                
            "energy":         {{ value_json.energy }},                                
            "frequency1":     {{ value_json.frequency1 }},                                
            "frequency2":     {{ value_json.frequency2 }},                                
            "frequency":      {{ value_json.frequency }},                                                                                                                                                                                                 
            "powerfactor1":   {{ value_json.powerfactor1 }},                                
            "powerfactor2":   {{ value_json.powerfactor2 }},                                
            "powerfactor":    {{ value_json.powerfactor }}                                
          }                                
        expire_after: 600                                    
        icon: "mdi:home-lightning-bolt" 

EDIT:

Oh, it’s the same with suggested precision. You can’t have that either. That fixed it!! Thanks!

1 Like