Use last attribuite instead of unavailable - Rest sensor

Hi i have this rest sensor:

- platform: rest
    name: wireguard
    resource_template: http://a0d7b954-wireguard
    json_attributes:
      - mi8
      - surface
      - x280
      - x280-linux
      - fujitsu-tower
    value_template: "OK"

and one template_sensor for every wireguard client:

platform: template
sensors:
  wireguard_client01:
    friendly_name: "Wireguard Client01"
    value_template: >-
        {% if state_attr('sensor.wireguard', 'client01').latest_handshake > ( as_timestamp(now()) ) -180 %}
            Connected
          {% else %}
            Disconnected
          {% endif %}

This rest_sensor sometime get unavailable
How can i not show unavailable but show his last state?

1 Like

As far as I know you can’t.

But you can set up another sensor based on that one to then filter out unavailable results in the subsequent sensor by use of a value_template.

Instead you should fix the sensor so it doesn’t become unavailable.

You can however create a template sensor that ignores the unavailable state.

sensor:
  - platform: template
    sensors:
      wireguard_filtered:
        friendly_name: "Wireguard Filtered"
        value_template: >
          {% if states('sensor.wireguard') in ['unavailable', 'unknown', 'none'] %}
            {{ states('sensor.wireguard_filtered') }}
          {% else %}
            {{ states('sensor.wireguard') }}
            {% endif %}

Note: I have not tried this method since the new template listner discovery method was implemented and it may complain about being self referential.

I think that is not working, because it continue to show unavailable.
Also, the original sensor save the information that i need in the attributes, how can i also save that in the filtered sensor?

I know. The problem seems to be related to the update 0.117, because other user have the same problem.
https://community.home-assistant.io/t/home-assistant-community-add-on-wireguard/134662/183?u=livioavalle
Maybe you can help me? What info you need?

Can you give me other infos? I need to save the attributes also

It shows OK.

You would have to create template sensors for each attribute.

I corrected your code, now it shows ok also for me, was unavialable.

Can you give me an example?

Would you mind telling me what you corrected, so I don’t do it again?

Same as for the template above except use state_attr(... see: Templating - Home Assistant

I would as well interested how you changed it. can you please give an example?

got it and changed my sensors now to

      wireguard_iphonepro:
        friendly_name: "Wireguard iPhone 11 Pro"
        value_template: >-
          {% if is_state('sensor.wireguard', 'unavailable') %}
            {{ states('sensor.wireguard_iphonepro') }}
          {% else %}
            {% if state_attr('sensor.wireguard', 'iphonepro').latest_handshake > ( as_timestamp(now()) ) -300 %}
              Verbunden
            {% else %}
              kein VPN
            {% endif %}
          {% endif %}

Yes, I will tell you.
Your code was that:

sensor:
  - platform: template
    sensors:
      wireguard_filtered:
        friendly_name: "Wireguard Filtered"
        value_template: >
          {% if states('sensor.wireguard') in ['unavialable', 'unknown', 'none'] %}
            {{ states('sensor.wireguard_filtered') }}
          {% else %}
            {{ states('sensor.wireguard') }}
            {% endif %}

I’ve fixed changing from
{% if states('sensor.wireguard') in ['unavialable', 'unknown', 'none'] %}
to
{% if states('sensor.wireguard') in ['unavailable', 'unknown', 'none'] %}
It was only a typo error.

But anyway that code doesn’t solve my problem. Because it keep only the state.
My error was also that i didnt give you my second template sensor that extract data from the attributes of sensor.wireguard and divide in every client name

Fortunately deluxestyle helped me, your code is working, thx

He modified the code that I also were using, from the Wireguard addons topic I think, thx

2 Likes

the sensors are working fine. but with the rest sensor I get a lot of following error message

Error fetching data: http://a0d7b954-wireguard failed with [Errno 111] Connect call failed ('172.30.33.4', 80)

I get the same. From what i’ve discovered, It seems related to latest update, 0.117. if you Watch on the GitHub issue page, there are some issue about rest sensor and 0.117

let’s hope for an fix with the next releases

1 Like

0.117.3 fixed it not. I still think it is related to the add-on.

Why do you think so? The changelog says that resource_template is fixed. Not resource. I would still wait to give up

The “call failed” is fine for me, if it is really not available, but I’m searching to an error handler in the sensor, so I can define, that this is o.k. instead of having these line in the error log.

Error fetching data: http://address failed with [Errno 111] Connect call failed ('192.168.178.21', 80)
Error fetching data: http://address  failed with [Errno 113] Connect call failed ('192.168.178.21', 80)

Is there a way to define it somehow, that I can define, that - if the endpoint is not there - no error line are put in the log?