Help needed with Template Sensor (JSON)

No more drops for the last 12 hours with the new templating vs the Old templating.
THANKS!!!

I will now post it at the Victron VRM forum, so everyone can get to use this great improvement from your templating!

thanks!!!

1 Like

If you can help me with this other one:

It is totally related,
I simplified the templating to this:
In this sensor it doesn’t output a value it outputs a text “alarm” o “no alarm”

{% for item in states.sensor.vrm_data.attributes.records if item["code"] == "dia" and item["instance"] == 1 %}{{ item["formattedValue"] }}{% endfor %}

I want to convert that text to: Alarma: Agua // Ok
I am trying with the following code, but doesn’t work.

  value_template: >-
  {% if state_attr('sensor.vrm_data.attributes.records', 'code') == "dia" and state_attr('sensor.vrm_data.attributes.records', 'instance') == 1 ['formattedValue'] == 'Alarm' %}
    Alarma: Agua
  {% else %}
    Ok
  {%- endif %}


You were close, but state_attr means access the state attributes, which you are also trying to access in the old way at the same time. Additionally you still need to loop through the results to find the matching code and instance. Something like this:

{% set x = state_attr('sensor.vrm_data','records') %}
{% set alarm = 0 %}
{% for result in x %}
  {% if result['code'] == "dia" and result['instance'] == 1 and result['formattedValue'] == "Alarm" %}
    {% set alarm = 1 %}
  {% endif %}
{% endfor %}
{{ 'Alarma: Agua' if alarm == 1 else 'OK' }}

Let me know if that works, I’ve only just woken up, I haven’t even had coffee or a smoke yet haha.

not working :S it outputs “Ok” for both status

Can you post the relevant snippet of JSON so I can see what we can match it against.

If I template this on the developer tools

{{ state_attr('sensor.vrm_data', 'records') }}

I get this (have a look at the last entry, needs to based in “code” and “instance”):

[
  {
    "idSite": xxxxxxx,
    "timestamp": 1637003894,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 641,
    "description": "Bad non-unique identifier",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "o",
    "bitmask": 0,
    "formattedValue": "02xxxxxa",
    "id": 1
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 1,
    "description": "gatewayID",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "d",
    "bitmask": 0,
    "formattedValue": "OS",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "VGR, VGR2 or VER",
        "valueEnum": 0
      },
      {
        "nameEnum": "OS",
        "valueEnum": 1
      },
      {
        "nameEnum": "OS",
        "valueEnum": 2
      },
      {
        "nameEnum": "LoRaWAN lopy",
        "valueEnum": 3
      },
      {
        "nameEnum": "PHYSEE NodeMCU",
        "valueEnum": 4
      }
    ],
    "id": 2
  },
  {
    "idSite": xxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 386,
    "description": "Productid",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "mi",
    "bitmask": 0,
    "formattedValue": "xxxxx",
    "id": 3
  },
  {
    "idSite": xxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 387,
    "description": "MachineName",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "mn",
    "bitmask": 0,
    "formattedValue": "xx xxxx",
    "id": 4
  },
  {
    "idSite": xxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 2,
    "description": "Fw Version",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "v",
    "bitmask": 0,
    "formattedValue": "v2.73",
    "id": 5
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 3,
    "description": "Fw Build date",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "build",
    "bitmask": 0,
    "formattedValue": "20210830181758",
    "id": 6
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 556,
    "description": "Machine serial number",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "ms",
    "bitmask": 0,
    "formattedValue": "xxxxxx",
    "id": 7
  },
  {
    "idSite": xxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 453,
    "description": "Capabilities",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "cp",
    "bitmask": 1,
    "formattedValue": "xxxxxxxx",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "MxxxxxPC",
        "valueEnum": 1
      },
      {
        "nameEnum": "xxxxxxmand",
        "valueEnum": 2
      },
      {
        "nameEnum": "Vxxxxion",
        "valueEnum": 4
      },
      {
        "nameEnum": "Vxxxxxnk",
        "valueEnum": 8
      }
    ],
    "id": 8
  },
  {
    "idSite": xxxxx,
    "timestamp": 1634301837,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 237,
    "description": "Boot type",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "w",
    "bitmask": 0,
    "formattedValue": "xxxxxx.",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "Tmpxxxxx",
        "valueEnum": "-3"
      },
      {
        "nameEnum": "Rexxxxiled",
        "valueEnum": "-2"
      },
      {
        "nameEnum": "Reaxxxxxiled",
        "valueEnum": "-1"
      },
      {
        "nameEnum": "Old kexxxxort, etc.",
        "valueEnum": 0
      },
      {
        "nameEnum": "Cxxxxxxot",
        "valueEnum": 1
      },
      {
        "nameEnum": "UnrxxxxxxGX",
        "valueEnum": 2
      },
      {
        "nameEnum": "Reset button",
        "valueEnum": 3
      },
      {
        "nameEnum": "Cold boot",
        "valueEnum": 4
      },
      {
        "nameEnum": "Reboot command",
        "valueEnum": 5
      },
      {
        "nameEnum": "Watchdog reboot",
        "valueEnum": 17
      },
      {
        "nameEnum": "Maxxxxxxx2.06)",
        "valueEnum": 29997
      },
      {
        "nameEnum": "Watchdog-ENOMEM",
        "valueEnum": 30012
      },
      {
        "nameEnum": "Watchdog-EMAXLOAD (load average too high)",
        "valueEnum": 30253
      }
    ],
    "id": 9
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1636292722,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 154,
    "description": "Remote support",
    "formatWithUnit": "%s",
    "dbusServiceType": "settings",
    "dbusPath": "/Settings/System/RemoteSupport",
    "code": "rss",
    "bitmask": 0,
    "formattedValue": "xxxxx",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "xxxxx",
        "valueEnum": 0
      },
      {
        "nameEnum": "xxxxxx",
        "valueEnum": 1
      }
    ],
    "id": 10
  },
  {
    "idSite": xxxxxxx,
    "timestamp": 1636948947,
    "Device": "Gateway",
    "instance": 0,
    "idDataAttribute": 543,
    "description": "Remote support IP and port",
    "formatWithUnit": "%s",
    "dbusServiceType": "settings",
    "dbusPath": "/Settings/System/RemoteSupportIpAndPort",
    "code": "rsIpPort",
    "bitmask": 0,
    "formattedValue": "xx.x.xx.xx;xxxxxx",
    "id": 11
  },
 {
    "idSite": xxxxxx,
    "timestamp": 1636292722,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 637,
    "description": "Temperature custom name",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tscn",
    "bitmask": 0,
    "formattedValue": "Nevera Arcon",
    "id": 173
  },
  {
    "idSite": xxxxxxx,
    "timestamp": 1636292722,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 445,
    "description": "Product ID",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tsP",
    "bitmask": 0,
    "formattedValue": "xxxxx",
    "id": 174
  },
  {
    "idSite": xxxxxxx,
    "timestamp": 1636292722,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 447,
    "description": "Temperature scale factor",
    "formatWithUnit": "%.1F",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tsc",
    "bitmask": 0,
    "formattedValue": "1.0",
    "id": 175
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1636292722,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 448,
    "description": "Temperature offset",
    "formatWithUnit": "%.0F",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tso",
    "bitmask": 0,
    "formattedValue": "8",
    "id": 176
  },
  {
    "idSite": xxxxxx,
    "timestamp": 1636292722,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 449,
    "description": "Temperature type",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tst",
    "bitmask": 0,
    "formattedValue": "Fridge",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "Battery",
        "valueEnum": 0
      },
      {
        "nameEnum": "Fridge",
        "valueEnum": 1
      },
      {
        "nameEnum": "Generic",
        "valueEnum": 2
      }
    ],
    "id": 177
  },
  {
    "idSite": xxxxxxx,
    "timestamp": 1637004314,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 450,
    "description": "Temperature",
    "formatWithUnit": "%.1F °C",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tsT",
    "bitmask": 0,
    "formattedValue": "12.0 °C",
    "id": 178
  },
  {
    "idSite": xxxxxxx,
    "timestamp": 1637004314,
    "Device": "Temperature sensor",
    "instance": 26,
    "idDataAttribute": 451,
    "description": "Temperature status",
    "formatWithUnit": "%s",
    "dbusServiceType": null,
    "dbusPath": null,
    "code": "tsS",
    "bitmask": 0,
    "formattedValue": "OK",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "OK",
        "valueEnum": 0
      },
      {
        "nameEnum": "Disconnected",
        "valueEnum": 1
      },
      {
        "nameEnum": "Short circuited",
        "valueEnum": 2
      },
      {
        "nameEnum": "Reverse Polarity",
        "valueEnum": 3
      },
      {
        "nameEnum": "Unknown",
        "valueEnum": 4
      }
    ],
    "id": 179
  }

{
    "idSite": xxxxxx,
    "timestamp": 1637154971,
    "Device": "Digital input",
    "instance": 1,
    "idDataAttribute": 465,
    "description": "Digital input alarm",
    "formatWithUnit": "%s",
    "dbusServiceType": "digitalinput",
    "dbusPath": "/Alarm",
    "code": "dia",
    "bitmask": 0,
    "formattedValue": "No alarm",
    "dataAttributeEnumValues": [
      {
        "nameEnum": "No alarm",
        "valueEnum": 0
      },
      {
        "nameEnum": "Warning",
        "valueEnum": 1
      },
      {
        "nameEnum": "Alarm",
        "valueEnum": 2
      }
    ],
    "id": 182
  },

]

I have managed to make it work using a “raw” sensor, and then a sensor that reads the previous raw sensor and converts the values.

It’s because of scopes. I have to declare a namespace in order to allow the for loop to set the variable, otherwise the variable only exists inside the for loop:

{% set alarm = namespace(alarm=[]) %}
{% set alarm.alarm = 0 %}
{% for result in x %}
  {% if result['code'] == "dia" and result['instance'] == 1 and result['formattedValue'] == "Alarm" %}
    {% set alarm.alarm = 1 %}
  {% endif %}
{% endfor %}
{{ 'Alarma: Agua' if alarm.alarm == 1 else 'OK' }}

Tested :slight_smile:

The same issue would apply to the temperature sensor code too - the one that sets the t variable which is then tested after the loop to see if it is above -99. So quick edit:

{% set t = namespace(temp=[]) %}
{% set t.temp = -99 %}
{% for result in il %}
  {% if result['instance'] == 26 and result['code'] == "tsT" %}
    {% set t.temp = result['formattedValue'].split(" ")[0]|float(-99) %}
  {% endif %}
{% endfor %}
{% if t.temp > -99 %}
  {{ t.temp }}
{% else %}
  Unavailable
{% endif %}

Note that I have swapped from looking for description of temperature to using the code tsT because I had a look here: VRM API v2 Documentation | Victron Energy documentation where they list ALL the possible codes

That’s right, tat is the one,

but men, I tried this one and stays always as “Ok”,

{% set alarm = namespace(alarm=[]) %}

{% set alarm.alarm = 0 %}

{% for result in x %}

  {% if result['code'] == "dia" and result['instance'] == 1 and result['formattedValue'] == "Alarm" %}

    {% set alarm.alarm = 1 %}

  {% endif %}

{% endfor %}

{{ 'Alarma: Agua' if alarm.alarm == 1 else 'OK' }}

I have managed t do it by the following,

first this sensor:

  - platform: template

    sensors:

      #Sentina triaca
      triaca_bilge_raw:
        friendly_name: "Sentina raw"
        icon_template: mdi:water
        value_template: '{% for item in states.sensor.vrm_data.attributes.records if item["code"] == "dia" and item["instance"] == 1 %}{{ item["formattedValue"] }}{% endfor %}'


Ant then With this one that reads the previous.

  - platform: template
    sensors:
      triaca_bilge:
        friendly_name: "Sentina"
        icon_template: mdi:water
        value_template: >-
          {% if is_state('sensor.triaca_bilge_raw', 'No alarm') %}
            Ok
          {% else %}
            Alarma: Agua
          {% endif %}

When you tried it, did it still have the line:

{% set x = state_attr('sensor.vrm_data','records') %}

Otherwise there would be no results in x and the loop wouldn’t run… I realised I forgot to include it in the revised template…

{% set x = state_attr('sensor.vrm_data','records') %}
{% set alarm = namespace(alarm=[]) %}
{% set alarm.alarm = 0 %}
{% for result in x %}
  {% if result['code'] == "dia" and result['instance'] == 1 and result['formattedValue'] == "Alarm" %}
    {% set alarm.alarm = 1 %}
  {% endif %}
{% endfor %}
{{ 'Alarma: Agua' if alarm.alarm == 1 else 'OK' }}

This one is Working!!!
GreaT!

1 Like