Custom Sensor template error

Hello all,
I have this code in my sensors.yaml file and I keep receiving errors. Can you please help?
The code should get the response in the TCP platform, and then in the template, it should return 2 values.

- platform: tcp
  name: 4heat_response
  host: 192.168.0.14
  port: 80
  timeout: 5
  payload: '["SEL","0"]'
  unit_of_measurement: "°C"

- platform: template
  sensors:
    Stove_temp0:
      entity_id: sensor.4heat_water_temp
      name: "4heat Water Temperature"
      value_template: "{{ int(states('4heat_response')[states('4heat_response').find('J30017')+6:states('4heat_response').find('J30017')+18])) }}"
    Stove_Temp1:
      entity_id: sensor.4heat_boiler_set_temp
      name: "4heat Boiler Target Temperature"
      value_template: "{{ int(states('4heat_response')[states('4heat_response').find('B20180')+6:states('4heat_response').find('B20180')+18])) }}"

The error I receive when i validate the configuration is:

Invalid config for [sensor.template]: invalid slug Stove_temp0 (try stove_temp0) for dictionary value @ data[‘sensors’]. Got OrderedDict([(‘Stove_temp0’, OrderedDict([(‘entity_id’, ‘sensor.4heat_water_temp’), (‘name’, ‘4heat Water Temperature’), (‘value_template’, “{{ int(states(‘4heat_response’)[states(‘4heat_response’).find(‘J30017’)+6:states(‘4heat_response’).find(‘J30017’)+18])) }}”)])), (‘Stove_Temp1’, OrderedDict([(‘entity_id’, ‘sensor.4heat_boiler_set_temp’), (‘name’, ‘4heat Boiler Target Temperature’), (‘value_template’, "{{ int(states(‘4heat_response’)[states(‘4heat_response’).find(‘B20180’)+6:states('4hea… (See ?, line ?).

You cant use capital letters here:

- platform: template
  sensors:
    Stove_temp0:

Do this instead:

- platform: template
  sensors:
    stove_temp0:

Same for Stove_temp1

1 Like

In addition, be advised that Template Sensors no longer use the entity_id option (it was deprecated many versions ago).

1 Like

I did change the case of the letters and I also i changed the name: to friendly_name:

now i have this code

- platform: template
  sensors:
    stove_temp0:
      friendly_name: "4heat Boiler Water Temperature"
#      entity_id: sensor.4heat_water_temp
      value_template: "{{ int(states('4heat_response')[states('4heat_response').find('J30017')+6:states('4heat_response').find('J30017')+18])) }}"
    stove_temp1:
#      entity_id: sensor.4heat_boiler_set_temp
      friendly_name: "4heat Boiler SET Temperature"
      value_template: "{{ int(states('4heat_response')[states('4heat_response').find('B20180')+6:states('4heat_response').find('B20180')+18])) }}"

and this error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘sensors’][‘stove_temp0’][‘value_template’]. Got “{{ int(states(‘4heat_response’)[states(‘4heat_response’).find(‘J30017’)+6:states(‘4heat_response’).find(‘J30017’)+18])) }}”
invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘sensors’][‘stove_temp1’][‘value_template’]. Got “{{ int(states(‘4heat_response’)[states(‘4heat_response’).find(‘B20180’)+6:states(‘4heat_response’).find(‘B20180’)+18])) }}”. (See ?, line ?).

This is invalid:

states('4heat_response')

You have to supply the states() function with a proper entity_id.

For example:

states('sensor.4heat_response')

For more information, refer to this section in the documentation.


Can you share an example of the state value of sensor.4heat_response? I am curious to know what kind of data that template is processing.

One of the responses from the controller is this one:

[“SEL”,“11”,“J30001000000000000”,“J30002000000000000”,“J30005000000000021”,“J300 06000000000000”,“J30011000000000110”,“J30012000000000000”,“J30017000000000022”," J30020000000000000",“B20180000000000066”,“B20005000000000030”,“B20006000000000080”]

this is the topic where i share all the data

As an experiment, copy-paste this into the Template Editor and see what it reports:

{{ (states('sensor.4heat_response') | from_json)[8] }}
1 Like

it is working ! I found one more extra ‘)’ at the end

image

Thanks a lot @123 and @tom_l

This is my first contribution to the community. Do you think it takes a lot of time to make it into custom integration since I already practically have the whole communication on the controller?

I hope you realize that an entity’s state value can only contain a string that’s 255 characters or less. The payload example you provided is 246 characters long. If that’s the longest it ever gets then you’re fine. If it ever exceeds 255, the excess is discarded.

I think it is fine, coz the controller seems to send all the time same string length. He is using the zeros as free space. I noticed that when the exhaust temperature was 140 degrees.