Help needed with Template Sensor (JSON)

this might work:

          {% set il = state_attr('sensor.vrm_data', 'records') %}
          {% for i in il %}
            {% if i['description'] == 'Fw Build date' %}
              {{ i['formattedValue'] }}
            {% endif %}
          {% endfor %}

The problem with description is that the I have some items with the same description.

I am trying with “instance” or with “iddataattribute” as follows:

          {% set il = state_attr('sensor.vrm_data', 'records') %}
          {% for i in il %}
            {% if i['instance'] == '26' %}.
              {{ i['formattedValue'] }}
            {% endif %}
          {% endfor %}

But the problem is that I get nothing, I feel like the number should be formatted differently.

Please put this in the template editor (Developer Tools / Template), and paste the result:

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

Then explain which element you are trying to extract. Your original paste is formatted as YAML not JSON.

It is sensible info, I can’t past the entire JSON, way to long to review it,

I paste the following section, that I can review and XXX mark the sensible info

IDS change, so are not usable, but IdData Attribute is unique (I think so far) and Instance is not unique to a single device, it repeats across multiple devices,

Maybe match a IdDataAttribute, or
another option could be to match both, an Instance and the description of that instance, this would be the most precise method, to look for the formatted value of an instance and a description.

An instance (eg:temp sensor) has multiple descriptions (senso type, sensor offset, sensor value)

What I want to extract for example is the fridge temp (AS A FLOAT VALUE!!!) which is in the ID 178,
Prevously extracted via this which gives errors when the JSON suddenly includes new IDs or publishes them in different order:

sensor:
  - platform: template

    sensors:
      triaca_fridge_temp:
        friendly_name: "Temp. Nevera"
        icon_template: mdi:thermometer
        unit_of_measurement: "ºC"
        value_template: '{{ states.sensor.vrm_data.attributes.records[177]["formattedValue"][:-2] | float () }}'

[
  {
    "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
  }
]

What is the idDataAtrribute of the value that you want? Does this stay the same regardless of the order of the JSON?

There are many that I want, but I can work around with one working to create the others

  • ID changes too much (around 30 times a day, fluctuates to another and then comes back to the original one)
  • Not sure about IdDataAttribute, I think it doesn’t change
  • instance + Device/description doesn’t change.

I want to get the Value 12.0 as a float value.

  {
    "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
  },
{% for result in il %}
  {% if result['instance'] == 26 and result['description'] == "Temperature" %}
    {{ result['formattedValue'].split(" ")[0]|float(-99) }}
  {% endif %}
{% endfor %}

This is what split returns, so you understanding what is happening:

[
  "12.0",
  "°C"
]
1 Like

Great! It works!
I see what you have done there!
can you point me towards some reading regarding “float(-99)”, I don’t get why you add a “-99” there.

Float (and other filters) now need to be provided with a default value which they will return if the value couldn’t be parsed, otherwise in the December(I think it is) Home Assistant update, the template will just fail to render at all.

By using this - you could do something like this:

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

And if Unavailable is not ideal, then you could simply give the current state instead with

{{ states('sensor.triaca_fridge_temp')|float(0) }}

great!, thanks for the info!

Lets see if the JSON outputs properly, and behaves properly and I down have any drops during the night, then I’ll template other sensors!

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