bigbadblunt / homeassistant-lightwave2

Lightwave RF custom component for Home Assistant. Requires generation 2 ("Link Plus") hub, but will control both generation 1 ("Connect Series") and generation 2 ("Smart Series") devices.
41 stars 10 forks source link

Feature Request: polling device #77

Closed SGXander closed 1 year ago

SGXander commented 2 years ago

The Non-public API is still the better of the two but even with the timed restart function still drops out for large chunks of time prompting a HA restart to fix. Could a device be chosen instead as a "monitor" device that would force a reconnection if it didn't report in for some time? If this was watching the consumption meter on the socket powering my HA server for example this would be a good way of detecting the connection drop and restarting.

bigbadblunt commented 2 years ago

How about i add a service call to force a reconnect? It would then be up to you to decide on the logic for when to invoke that call?

SGXander commented 2 years ago

That's even better, This way if they haven't reported in then it can be done with an automation and should be minimal outage too. I have it set to 28 minutes at the moment which seems to be the best time interval but between 21:00 and 07:00 it still seems to lose feedback completely so hoping this will fix it.

I assume LWRF haven't responded about the heartbeat?

gary-sargent commented 2 years ago

Don't you get a webhook message when you change something? Could take something rarely changed like the lock status, and send a request to set the value to the same value as it already has. Then we'd expect a webhook response back saying it was updated. If we don't get it, we know it's gone funny. This could be a pseudo heartbeat.

xela1 commented 2 years ago

The websocket stays open and responds to state changes, it just stops sending state updates, the only way really would be to check when you've not received any data in x minutes/hours etc

gary-sargent commented 2 years ago

Yes but you'd get a state update from making a change, or expect to. The socket might be open enough to send a command, but if you don't get back the change repeated, you know something is wrong.

xela1 commented 2 years ago

Oh I understand what you're saying now, I thought you meant the response from your request, but you mean a state change triggered from your command instead, got it. That would work, not sure how easy to implement though

bigbadblunt commented 2 years ago

OK, I have done two things

1) added a service lightwave2.reconnect that forces a reconnect 2) added a sensor to the link that shows the last time an event was received (see screenshot below)

this should allow you to write whatever automation you'd like to send a signal and check for a response.

image

garysargentpersonal commented 2 years ago

@bigbadblunt any chance the sensor could have "seconds since last event received" rather than the timestamp? Would mean we don't have to attempt nasty date/time calculation just to see how long ago the last event was.

garysargentpersonal commented 2 years ago

Ok I have setup one automation which is triggering using "time pattern" using minutes as "/1" to run every minute. The actions are turn the LinkPlus LED off, then on again. This causes events to be received on the webhook.

Then I have a second automation which is a template trigger as follows:

{{ (as_timestamp(now()) - as_timestamp(states.sensor.linkplus_last_event_received.state)) | int > 140 }}

When that fires (last event was more than 140 seconds ago) it sends me a notification, and calls the force reconnect.

I've configured the integration with a value of 0 so it doesn't do its own force reconnection.

Let's see how that goes...

bigbadblunt commented 2 years ago

Changing to "seconds since last event" is a bit problematic as then I have to keep updating the sensor every second. I'm not sure how to do that safely.

Are you using the non-public API? The reconnect service only reconnects the non-public websocket so won't do anything on the public API.

garysargentpersonal commented 2 years ago

Ah yes good point on having to keep updating the sensor.

Yes I'm using the non-public API.

garysargentpersonal commented 2 years ago

Here is my full automation in case it helps anyone...

alias: Reconnect to Lightwave when no events received
description: ''
trigger:
  - platform: template
    value_template: >
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >
      140 }}
    id: events_not_received
  - platform: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >
      240 }}
    id: events_still_not_received
  - platform: time_pattern
    id: force_some_events
    minutes: /1
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: events_not_received
        sequence:
          - service: notify.pushover
            data:
              message: >-
                Stopped receiving Lightwave events in Home Assistant. Forcing
                reconnection.
          - service: lightwave2.reconnect
            data: {}
      - conditions:
          - condition: trigger
            id: events_still_not_received
        sequence:
          - service: notify.pushover
            data:
              message: >-
                Not receiving Lightwave events even after forcing a
                reconnection. Please restart HA.
      - conditions:
          - condition: trigger
            id: force_some_events
        sequence:
          - type: turn_off
            device_id: 0009161e043d5b7ffed765ed8f8be954
            entity_id: light.linkplus_led
            domain: light
          - type: turn_on
            device_id: 0009161e043d5b7ffed765ed8f8be954
            entity_id: light.linkplus_led
            domain: light
    default: []
mode: queued
SGXander commented 2 years ago

I'm going to give this a try when I get back but thank you for this! The only issue I can think of is that when reconnecting can you re-enumerate device state? I have been seeing a problem where the auto reconnect works but any physical presses while it was disconnected are not updated after reconnecting.


From: Gary Sargent @.> Sent: Sunday, May 15, 2022 2:05:09 PM To: bigbadblunt/homeassistant-lightwave2 @.> Cc: SGXander @.>; Author @.> Subject: Re: [bigbadblunt/homeassistant-lightwave2] Feature Request: polling device (Issue #77)

Here is my full automation in case it helps anyone...

alias: Reconnect to Lightwave when no events received description: '' trigger:

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fbigbadblunt%2Fhomeassistant-lightwave2%2Fissues%2F77%23issuecomment-1126936998&data=05%7C01%7C%7Cf062f2ed297d4c8a07d308da36738a8e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637882167108407814%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=sIhe3VgEvpEQFs3IBHkIXOkUmzE%2BaT%2FvcoZ4mLTmCio%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACBYMEHF6GSOK2Y33HLERTLVKDZALANCNFSM5VETABRQ&data=05%7C01%7C%7Cf062f2ed297d4c8a07d308da36738a8e%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637882167108407814%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=lFl6YVMv1fItbapRoxVzfr6hbEK9jAOvsr5MEUxJDX4%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

RGarrett93 commented 2 years ago

@garysargentpersonal Thanks for sharing this, its a well written automation and seems to be working great for automatically reconnecting. I was trying to work out a solution for auto reconnect and this automation solves it.

@bigbadblunt Thank you for pushing so many updates and for this great integration! It might worth referencing 'lightwave2.reconnect' and the above automation as it seems to be a good work around.

SGXander commented 2 years ago

+1 for @garysargentpersonal, using this pretty much as-is. Fingers crossed this sorts it!

@bigbadblunt the Link Plus LED on/off is like the Gen1 and not a toggle, not sure if it should be but the other G2 gear LEDs are.

SGXander commented 2 years ago

Screenshot_20220516-070328_Home Assistant

So good news is it works^ I'm going to have a tweak later as it seems to fail if the connection is dropped before midnight but the next check is after midnight (something to do with the conversion maybe?)

Bad is that it still seems to die eventually though I think this may be down to it being in one automation? I'm going to split it out into 2 and see if that fixes it given the time stamps. Will also make sure I get logs next time it drops out.

garysargentpersonal commented 2 years ago

Interesting as when I compare to my outages - they are pretty much the same times as yours!

07:48:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
07:47:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
06:45:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
06:44:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
06:39:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
06:38:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
05:36:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
05:34:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
04:32:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
04:31:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
04:28:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
04:27:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
03:25:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
03:23:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
02:21:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
02:20:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
02:15:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
02:14:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
01:12:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
01:10:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
00:07:06 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
00:07:00 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received
00:03:19 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received
00:03:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

I now have a boolean helper entity that is true when events are being received, and false when not - this means I get a logbook entry and history of all changes in state.

I've also made the automation continue to send reconnect calls every minute after the initial one when it hasn't connected. I've had some instances where the first reconnect didn't work, but a subsequent one would.

alias: Reconnect to Lightwave when no events received
description: ''
trigger:
  - platform: template
    value_template: >
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >
      140 }}
    id: events_not_received
  - platform: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >
      600 }}
    id: events_still_not_received
  - platform: time_pattern
    id: force_some_events
    minutes: /1
  - platform: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int <
      140 }}
    id: events_received
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: events_not_received
        sequence:
          - service: notify.pushover
            data:
              message: >-
                Stopped receiving Lightwave events in Home Assistant. Forcing
                reconnection.
          - service: lightwave2.reconnect
            data: {}
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.lightwave_events_connected
      - conditions:
          - condition: trigger
            id: events_still_not_received
        sequence:
          - service: notify.pushover
            data:
              message: >-
                Not receiving Lightwave events even after forcing a
                reconnection. Please restart HA.
      - conditions:
          - condition: trigger
            id: force_some_events
        sequence:
          - type: turn_off
            device_id: 0009161e043d5b7ffed765ed8f8be954
            entity_id: light.linkplus_led
            domain: light
          - type: turn_on
            device_id: 0009161e043d5b7ffed765ed8f8be954
            entity_id: light.linkplus_led
            domain: light
          - delay:
              hours: 0
              minutes: 0
              seconds: 5
              milliseconds: 0
          - if:
              - condition: template
                value_template: >-
                  {{ (as_timestamp(now()) -
                  as_timestamp(states.sensor.linkplus_last_event_received.state))
                  | int > 240 }}
            then:
              - service: lightwave2.reconnect
                data: {}
      - conditions:
          - condition: trigger
            id: events_received
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.lightwave_events_connected
    default: []
mode: queued
SGXander commented 2 years ago

That is creepy! especially when they are only a couple of minutes apart as well it's almost like something is disconnecting users en masse!?

That's nice, I'll copy that a bit later on. More data can only help and maybe we can identify the issue for LWRF to fix.

SGXander commented 2 years ago

Well this was running fine most of the day then, sods law, my Link Plus has died completely! I'll be back testing in a few days assuming they replace it.

astirling01 commented 2 years ago

Interesting as when I compare to my outages - they are pretty much the same times as yours!


07:48:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

07:47:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

06:45:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

06:44:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

06:39:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

06:38:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

05:36:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

05:34:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

04:32:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

04:31:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

04:28:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

04:27:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

03:25:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

03:23:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

02:21:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

02:20:06 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

02:15:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

02:14:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

01:12:05 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

01:10:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

00:07:06 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

00:07:00 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

00:03:19 - Lightwave Events Connected turned on by Reconnect to Lightwave when no events received

00:03:05 - Lightwave Events Connected turned off by Reconnect to Lightwave when no events received

I now have a boolean helper entity that is true when events are being received, and false when not - this means I get a logbook entry and history of all changes in state.

I've also made the automation continue to send reconnect calls every minute after the initial one when it hasn't connected. I've had some instances where the first reconnect didn't work, but a subsequent one would.


alias: Reconnect to Lightwave when no events received

description: ''

trigger:

  - platform: template

    value_template: >

      {{ (as_timestamp(now()) -

      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >

      140 }}

    id: events_not_received

  - platform: template

    value_template: >-

      {{ (as_timestamp(now()) -

      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int >

      600 }}

    id: events_still_not_received

  - platform: time_pattern

    id: force_some_events

    minutes: /1

  - platform: template

    value_template: >-

      {{ (as_timestamp(now()) -

      as_timestamp(states.sensor.linkplus_last_event_received.state)) | int <

      140 }}

    id: events_received

condition: []

action:

  - choose:

      - conditions:

          - condition: trigger

            id: events_not_received

        sequence:

          - service: notify.pushover

            data:

              message: >-

                Stopped receiving Lightwave events in Home Assistant. Forcing

                reconnection.

          - service: lightwave2.reconnect

            data: {}

          - service: input_boolean.turn_off

            data: {}

            target:

              entity_id: input_boolean.lightwave_events_connected

      - conditions:

          - condition: trigger

            id: events_still_not_received

        sequence:

          - service: notify.pushover

            data:

              message: >-

                Not receiving Lightwave events even after forcing a

                reconnection. Please restart HA.

      - conditions:

          - condition: trigger

            id: force_some_events

        sequence:

          - type: turn_off

            device_id: 0009161e043d5b7ffed765ed8f8be954

            entity_id: light.linkplus_led

            domain: light

          - type: turn_on

            device_id: 0009161e043d5b7ffed765ed8f8be954

            entity_id: light.linkplus_led

            domain: light

          - delay:

              hours: 0

              minutes: 0

              seconds: 5

              milliseconds: 0

          - if:

              - condition: template

                value_template: >-

                  {{ (as_timestamp(now()) -

                  as_timestamp(states.sensor.linkplus_last_event_received.state))

                  | int > 240 }}

            then:

              - service: lightwave2.reconnect

                data: {}

      - conditions:

          - condition: trigger

            id: events_received

        sequence:

          - service: input_boolean.turn_on

            data: {}

            target:

              entity_id: input_boolean.lightwave_events_connected

    default: []

mode: queued

Erm...do I just paste that into my automations yaml and everything will work fine?

SGXander commented 2 years ago

You'll need to change the devices to yours. easiest way is to paste as yaml into a new automation then switch back to the gui editor and change the link plus and notification devices.

SGXander commented 2 years ago

So after coaxing the Link Plus back to life (mini-overload?) this morning has been crazy. It's reconnecting every 3-5 minutes. I assume this is the result of more of us using the reconnect mechanism so it must be a general thing that reconnections affect others? @bigbadblunt any thoughts on how this could be happening?

I've logged an LWRF Plus troubleshooting hour to see if they will help at all for tomorrow at 09:00.

garysargentpersonal commented 2 years ago

Looks to be the same frequency as usual to me. The troubleshooting will be good - this really needs a fix their side.

image

astirling01 commented 2 years ago

You'll need to change the devices to yours. easiest way is to paste as yaml into a new automation then switch back to the gui editor and change the link plus and notification devices.

Ah good idea. The public api has been fine for me but it's interesting the non-public continued to function yesterday whilst Lightwave went down on both the public and their Alexa integration. Makes me keen to switch back if.... if I can get it stable, it may prove to be a more reliable option.

bigbadblunt commented 2 years ago

The only issue I can think of is that when reconnecting can you re-enumerate device state?

I've add (another!) service call update_states that re-reads the states from the server. This can take a little time for the websocket (non-public API), so I've kept it as an optional separate call.

ianphillpott commented 2 years ago

So after coaxing the Link Plus back to life (mini-overload?) this morning has been crazy. It's reconnecting every 3-5 minutes. I assume this is the result of more of us using the reconnect mechanism so it must be a general thing that reconnections affect others? @bigbadblunt any thoughts on how this could be happening?

I've logged an LWRF Plus troubleshooting hour to see if they will help at all for tomorrow at 09:00.

Did you get anywhere with LWRF on this? Im experiencing the same issue where by my radiators stop updating HA so probably going to have to impliment the script to stop it disconnecting....

bigbadblunt commented 1 year ago

I have updated the documentation/read me to point to this trail. I am going to close due to inactivity. I will keep #69 open until the underlying issue is fixed.

Valiante commented 1 year ago

I've also made the automation continue to send reconnect calls every minute after the initial one when it hasn't connected. I've had some instances where the first reconnect didn't work, but a subsequent one would.

Holy cow, this just works as-is. Thanks!