Which non json text sensor type to use for http command which returns 18 values?

I am wanting to just collect data from a HVAC unit, the only data it will return is:

opmode=0&units=.&settemp=25.0&fanspeed=3&fanflags=1&acmode=8&tonact=0&toffact=0&prog=0&time=07:44&day=1&roomtemp=34&outsidetemp=0&louvre=1&zone=0&flt=0&test=0&errdata=146&sensors=1

It is a bit clunky so I’d prefer to hit it up with one command, grab the values I want, and store/monitor them.

So back to the question - can someone please help me determine:

  1. which sensor type
  2. a good example so I can hack my way through it

Thanks

There might be a better way I’m not thinking of, but I’d do this. First, create whatever sensor is necessary to get that string into its state. (You didn’t say much about how you can obtain that string, so I assume you already have that part covered. If not, please provide more detail.) Let’s call this sensor sensor.hvac_raw, such that {{ states('sensor.hvac_raw') }} would return that string.

Then you can create a Template Sensor whose state is whatever value is most important (e.g., roomtemp), and which has attributes for other values you care about. Maybe something like:

sensor:
  - platform: template
    sensors:
      hvac:
        friendly_name: HVAC
        unit_of_measurement: '°C'
        value_template: >
          {{ states('sensor.hvac_raw')|regex_findall_index('roomtemp=([0-9.]+)') }}
        attribute_templates:
          op_mode: >
            {{ states('sensor.hvac_raw')|regex_findall_index('opmode=([0-9.]+)') }}
          set_temp: >
            {{ states('sensor.hvac_raw')|regex_findall_index('settemp=([0-9.]+)') }}
          fan_speed: >
            {{ states('sensor.hvac_raw')|regex_findall_index('fanspeed=([0-9.]+)') }}
1 Like

The following template converts the string into a JSON dictionary object:

{% set x = ('{"' ~ value.replace('=','":"').replace('&','"&"').split('&') | join(',') ~ '"}') | from_json  %}

It lets you easily access each item in the dictionary. However, to my knowledge, the only kind of sensor that can efficiently create attributes from that dictionary is MQTT Sensor using its json_attributes_template. It would look something like this:

  json_attributes_template: >-
    {% set x = ('{"' ~ value.replace('=','":"').replace('&','"&"').split('&') | join(',') ~ '"}') | from_json  %}
    {"opmode":"{{x.opmode}}",
     "fan_speed":"{{x.fan_speed}}",
     "acmode":"{{x.acmode}}",
     "set_temp":"{{x.set_temp}}"}

In a Template Sensor, each attribute is handled by a separate template option (see Phil’s example), so the string-to-object conversion would have to be repeated in each option … which is inefficient.

As for a Restful Sensor, itsjson_attributes option is unable to use a template so it cannot support this approach.

1 Like