Jinja2 for HACS Update

I am trying to get into the jinja2 templating for some sensors I would like to create.
I got hung up when I wanted to set a state for a binary sensor I created.
I am trying to create a binary_sensor that basically is true when there is an update for any of the HACS add-ons. I looked into a lot o the blueprints and automations that are out there, but I need something simpler for my dashboard.
I already have the HACS update, the Supervisor Update, etc. Need now the HACS Add-On update.
This is my development code:

{%- set dev = namespace() %} {%- set dev.name = '' %} 
{%- set dev.update = '' %}
{%- for devid in (integration_entities('hacs') | map('device_id') | list) %}
  {%- if state_attr('update.hacs_update', 'installed_version') != state_attr('update.hacs_update', 'latest_version') %}
      set states('binary_sensor.hacs_addon_update') = True
  {%- endif %}
{%- endfor %}

I need help in the set states('binary_sensor.hacs_addon_update') = True

How do I set a state for the binary_sensor in the code?
If I add {%, I get an error. (I am still working on when to use {% or {{, and other general syntax in jinja2)

There’s no service call to set a binary_sensor’s state. The integration that created the binary_sensor is responsible for maintaining its state.

There’s a python script posted elsewhere in the forum that can be used to force the binary_sensor’s state. However, this ‘forced’ value is temporary and doesn’t survive a restart (and is overwritten by the binary_sensor’s integration).

I suggest you design your template to report true/false and then use it in a Trigger-based Template Binary Sensor. Make its trigger either a Time Trigger or a Time Pattern Trigger, depending on how frequently you want to execute the template.

HACS has a sensor that counts the number of updates available. Use that.

state: "{{ states('sensor.hacs')|float(0) > 0 }}"

This will be true whenever there are updates available.

Also HACS does not have add-ons. Only integrations and frontend resources.

1 Like

Used the wrong term, you are right. I am referring to updates for the Integrations and front end resources.
But I do not have the sensor.hacs in my system. Is this something that needs to be enabled somewhere?

I create the binary_sensor as a template

#####
# Binary sensor turns on if there is an Add-on update
#####

platform: template
sensors:
  hacs_addon_update:
    friendly_name: 'HACS add-on updates available'
    unique_id: hacs_addon_update
    value_template: >

so was trying to set it using a template.

Not sure if I am even going about it the right way.

You do not set states directly.
You use service calls.

That’s a legacy format template configuration which doesn’t support triggers. Refer to the documentation in the link I posted above.

If you can’t find the update sensor for HACS, it may simply be hidden. Go to Settings > Devices, click on HACS and check the Sensors section for hidden sensors.

1 Like

This is my version:
image
And this is what I see when I go into the HACS device. I get the Update sensor, but this is for HACS I believe. I have a pending update for one of the integrations, so the sensor should be on at this moment:

Based on the feedback (thank you all), the path would be to create a template that outputs true or false, then a trigger to update the binary_sensor based on that.
I will post my code once I get it working today I hope.

Still not sure why I do not have the sensor.hacs which would solve all of this for me.

This is my addon check card for integrations:

type: markdown
title: Home Assistant Integrations
content: |-
  <table width=100%><tr><td><b>
  {%- set hacs_updates = states.update -%}
  {%- for hacs_update in hacs_updates -%}
    <tr><td><b>
    {%- if (hacs_update.state=='off') -%}
      <font color=green>
    {%- else -%}
      <font color=red>
    {%- endif -%}
    {{hacs_update.attributes.title}}</font></b></td><td>{{hacs_update.attributes.installed_version}}</td><td>
    {%- if (hacs_update.state=='off') -%}
      &nbsp;
    {%- else -%}
      {{hacs_update.attributes.latest_version}}
    {%- endif -%}
    </td></tr>
  {%- endfor -%}
  </table>

And this is my card for addons.

type: markdown
title: HACS Addons
content: |-
  {%- if is_state('sensor.hacs','0') -%}
    <b><font color=green>No updates</font></b>
  {%- else -%}
    <table width=100%>
    {%- set hacs_updates = state_attr('sensor.hacs','repositories') -%}
    {%- for hacs_update in hacs_updates -%} 
      <tr><td><b><font color=red>{{[hacs_update][0]['display_name']}}</b></font></td><td>{{[hacs_update][0]['installed_version']}}</td><td>{{[hacs_update][0]['available_version']}}</td></tr>
    {%- endfor -%}
    </table>
  {%- endif -%}
1 Like

Thanks @WallyR, I remain puzzled at to why I do not see the sensor.hacs in my system. That would solve everything I am trying to get done.

Is it disabled or hidden?

Neither. I put in the screenshots earlier. Can’t see any entities other than the update. entity. I am digging here now to try and figure it out vs my original intent which is nothing other than a workaround it seems

Do you have the newest version of HACS?

1.30.1 installed. It doesn’t show an update is available so I assumed I have the latest.
image

Has this sensor been replaced? I get the sensor as unavailable?

Thanks

Still working for me.

What do you see when you go to Settings → Devices & Services → HACS ?


Show not available.

If I turn experiment off it comes back.

If you have experimental features enabled then each integration gets it’s own update sensor.

1 Like