Debugging question -

The question I have does not require the answerer to understand the logic shown below the line, just showing the complexity below the line for context. (Sorry not necessarily HA development but it is development to an extent…)

For an involved template state sensor which shows the correct answer when the code is pasted into Developer tools → Templates, but it does not show correctly at runtime, how can I debug the sensor itself? Is there a way to temporarily insert lines within the logic of the sensor itself to put the values used for calculation in the syslog (I could set up a bunch of temporary input_text sensors and then populate them with details but that is a PITA) - or is there a better way?

Context is below for the curious but not required to answer the question ‘per say’ -


The below shown sensor in configuration.yaml - has streamlined logic within a sensor by actually comparing some sensor names to the content (known error messages that sometimes occur) of other sensors. The input_text sensors here are properly populated by curl commands from outside Home Assistant by it’s API.

The most recent error is always properly shown within “input_text.weewx_last_rest_call_failure”. If data had been received from a related sensor since then (meaning the subsequent rest api call to a different machine) had more recently been successful, the below sensor is supposed to compare the two anbd if the successful rest api call was more recent, then there is no more issue currently and this sensor should instead display ‘No current errors’. It used to work before I moved the logic from a command_line sensor to this one in streamlining my code. Long story short, it never shows “No current errors.” but always the content of the latest error input_text.weewx_last_rest_call_failure. So how to debug when Developer tools → templates show the same code working properly?

template:
  - trigger:
      - platform: state
        entity_id: input_text.weewx_last_rest_call_failure
    sensor:
      - name: "Weewx last REST call failure"
        state: >
          {% set data = states('input_text.weewx_last_rest_call_failure') | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
          {% set errortime = strptime(now().year ~ ' ' ~ ' '.join(states('input_text.weewx_last_rest_call_failure').split(' ')[0:3]),'%Y %b %d %H:%M:%S',data) | as_local %}
          {% set errordestination = (((states('input_text.weewx_last_rest_call_failure') | regex_findall('(.+): Failed to publish') | first).strip() | regex_findall(': (\w+)') | first).strip()).lower() %}
          {% set sensortoretry = 'sensor.weewx_last_sent_to_' + errordestination %}
          {% set thisyear = now().year %}
          {% set statessensorretry = states(sensortoretry) %}
          {% set joiner1 = ' '.join(states(sensortoretry).split(' ')[0:3]) %}
          {% set joiner2 = now().year ~ ' ' ~ joiner1 %}
          {% set sensorretriedtime = strptime(joiner2,'%Y %a %m/%d %I:%M:%S%p',data) | as_local %}
          {% if (errordestination == 'stationregistry') %}
          {{ 'WEEWX Station Registration' }}
          {% elif (sensorretriedtime > errortime) %}
          {{ 'No current errors' }}
          {% else %}
          {% set ts = data.year ~ ' ' ~ (states('input_text.weewx_last_rest_call_failure') | regex_findall('(.+)kruse-pi') | first).strip() %}
          {% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
          {{ time.strftime('%a %-m/%-d %-I:%M:%S%p') }}, {{ (states('input_text.weewx_last_rest_call_failure')|regex_replace(find='EDT \(([0-9]+)\):', replace='EDT:', ignorecase=False)).split("weewx.restx: ",1)[1] }}
          {% endif %}
        unique_id: weewx_last_rest_call_failure

No it isn’t. Moved.

Also your sensor does not need a trigger. The template will automatically update when that entity changes.

My bad, thank you - what would be the syntax for the top part of the sensor if not “trigger” (example?) ? And do you have any idea on the debugging part?

template:
  - sensor:
      - name: "Weewx last REST call failure"
        state: >
          {% set data = states('input_text.weewx_last_rest_call_failure') | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
          {% set errortime = strptime(now().year ~ ' ' ~ ' '.join(states('input_text.weewx_last_rest_call_failure').split(' ')[0:3]),'%Y %b %d %H:%M:%S',data) | as_local %}
          {% etc...
1 Like

Can’t you just use the “normal” logger setting to debug the template component? :slight_smile:

logger:
  logs:
    homeassistant.components.template: debug
1 Like