Restart HA on xiaomi gateway when change state

I have two xiaomi gateway and they go unavailable very offten. and then I have to restart HA with

sudo systemctl restart [email protected]

command to get back.

I want to do some automation with node red or HA automation to HA run this command when my xiaomi gateway change state to unavailable…

Please help…

This is normally due to issues with your wifi

try assigning a static Ip in your router to the xiaomi gateways

I have static ip for both gateway… before is running normally one week but now is go unavailable every two hours…

the automation would be
trigger
a state change, some xiaomi connectivity sensor
action
service: homeassistant.restart

but maybe restart your wifi before going the ha continuous restart route.

I try this automation but HA doesn’t restart anymore I must go to putty and run command line:

sudo systemctl restart [email protected]

are you in hassbian?

yes in hassbian

ok so need to do the following:
in command line
sudo visudo

then add this
youruser ALL=(ALL) NOPASSWD:SETENV: /bin/systemctl

then add a shell comand like this

  shell_command:
    restart_ha: sudo systemctl restart [email protected]

then add to your automation

service: shell_command.restart_ha