Have openhab send a Heartbeat to MQTT

Hi . This might be a question already asked .
I want to have say my openHAB send a heart beat to my MQTT server . and have a esp8266 subscribes to that topic. that can if the heart beat is missing say gives the Raspberry a reboot signal over the GPIO of the pi ? I want to have this run on all my pi’s

what do you think of this or am I setting my self up for a fail ?

Use node-red on you remote pis and use an MQTT node to subscribe to the heart-beat topic and an exec node to send the command sudo reboot

Use a cron rule and the MQTT action to send a heart beat mqtt message
install the MQTT action!!

rule "heart beat every minute"
when
    Time cron "1 0 0 ? * * *"
then
    publish("broker", "topic", "payload")
end

In node-red:

[
    {
        "id": "75de1188.8438d",
        "type": "mqtt in",
        "z": "609ba6ef.24ec1",
        "name": "",
        "topic": "topic",
        "qos": "0",
        "broker": "28001b6c.f6c784",
        "x": 110,
        "y": 80,
        "wires": [
            [
                "195f2dc9.f75c22"
            ]
        ]
    },
    {
        "id": "195f2dc9.f75c22",
        "type": "trigger",
        "z": "609ba6ef.24ec1",
        "op1": "1",
        "op2": "0",
        "op1type": "str",
        "op2type": "str",
        "duration": "65",
        "extend": false,
        "units": "s",
        "reset": "payload",
        "bytopic": "all",
        "name": "",
        "x": 290,
        "y": 80,
        "wires": [
            [
                "409e1ffc.a3e7f"
            ]
        ]
    },
    {
        "id": "409e1ffc.a3e7f",
        "type": "switch",
        "z": "609ba6ef.24ec1",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 470,
        "y": 80,
        "wires": [
            [
                "6e607723.d16088"
            ]
        ]
    },
    {
        "id": "6e607723.d16088",
        "type": "exec",
        "z": "609ba6ef.24ec1",
        "command": "sudo reboot",
        "addpay": false,
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "",
        "x": 650,
        "y": 80,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "28001b6c.f6c784",
        "type": "mqtt-broker",
        "z": "",
        "name": "broker",
        "broker": "brokerIP",
        "port": "1883",
        "clientid": "openhab-node-red",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

The first node subscribe to the topic and passes the payload to the next node. If the second node hasn’t received a payload of “payload” after 65s then it will send a payload of 0 to the next node. The next node will trigger only on a payload of 0. The last node will execute a command line.

I think you are better off using the heartbeat built into MQTT.

You can configure OH with a Last Will and Testament in mqtt.cfg. This configures the MQTT Broker to set up a heart beat between itself and OH. When OH fails to generate the heartbeat exchange, the Broker will publish the configured message to the configured topic.

Then set up your ESP8266 to subscribe to that topic. When it sees a message it knows, at least as far as the MQTT broker is concerned, that OH is no longer connected.

Ok . Would the mqtt then send a command to the esp to bounce the Raspberry pi ?
my other worry is would this be a risk as I have been seeing a lot of men card issues on the raspberry ? is there a safe way to have the esp restart the pi ?

The Lwt message would be that message.

Short of logging in and issuing the shutdown command, probably not. But if the RPi is already off the network then something is already broken. So you have to assess whether the risk of pulling the power through a relay is worth the risk of file system corruption.