My first working automation - feedback on my rule

Thanks all, and now putting it all together, just so that I do not highjack the OP thread (where @EBF actually asked for very concrete help). Here the slightly improved version of post #9 that would read:

rule “Ceiling_fan_ON”
when
Item XiaomiMiTemperatureHumiditySensorLivingRoom_Temperature received update
then
    // Do nothing if restart
    if (previousState == NULL) return; // You need persistence installed. This is to avoid the rule being run at start-up

   // Process what command to send
    var new_fanState = XiaomiMiSmartSocketPlugCeilingInLineFan_Power.state
    if (XiaomiMiTemperatureHumiditySensorLivingRoom_Temperature.state >= 25) 
        new_fanState = ON
    else if (XiaomiMiTemperatureHumiditySensorLivingRoom_Temperature.state <= 24)
        new_fanState = OFF

    // Send command only if there was a change
    if(XiaomiMiSmartSocketPlugCeilingInLineFan_Power.state != new_fanState) 
        XiaomiMiSmartSocketPlugCeilingInLineFan_Power.sendCommand(new_fanState)
end

I renamed the variable to read now new_fanState for no other reasons as to improve readability.

1 Like

Hijack away - I’m happy to see as many takes as possible to learn.

Hey vzorglub,

Wondering if you could help me with some further things on this. I’ve migrated all my Xiaomi items to MQTT using the Zigbee2MQTT bridge (https://github.com/Koenkk/zigbee2mqtt).

The bridge is working. I have the Xiaomi temperature sensor reporting in via MQTT and I have also paired the Xiaomi ceiling plug. I’m at a bit of a loss now as to how to activate the plug via MQTT.

My Xiaomi Ceiling Plug MQTT item is this:

Switch Xiaomi_Switch_State "In Line Fan Switch" {mqtt=">[broker:zigbee2mqtt/0x00158d000215fcfb/set:JSONPATH:state:ON],>[broker:zigbee2mqtt/0x00158d000215fcfb/set:JSONPATH:state:OFF:0]"}

I don’t think that’s right though. The payload to turn on/off the switch needs to be in JSON format and is either {“state”: “OFF”} or {“state”: "ON} to zigbee2mqtt/<DEVICE_ID>/set where <DEVICE_ID> in this instance is 0x00158d000215fcfb. If I send the on/off payload via MQTTLens, it does activate/deactivate the switch. It’s what I put in the rule that I’m failing at.

Any help would be appreciated.

Just for the record, the temperature item in MQTT is:

Number Xiaomi_Sensor_LR_Temp "Living Room Temperature [%.1f]" {mqtt="<[broker:zigbee2mqtt/0x00158d000210a82f:state:JSONPATH($.temperature)]" }

Which is working correctly in both Habpanel and Grafana.

Switch Xiaomi_Switch_State "In Line Fan Switch" {mqtt=">[broker:zigbee2mqtt/0x00158d000215fcfb/set:command:ON:{"state":ON} ], >[broker:zigbee2mqtt/0x00158d000215fcfb/set:command:OFF:{"state":OFF} ]"}

Thanks vzorglub, that works with my switch in Habpanel. I’m now going to incorporate that into the rule that this thread talks about and see if I can get that to work - I believe it is via the MQTT action addon and using the PublishItem command.

I’ll be back in a few days if I can’t work it out :slight_smile:

Okay, back from holidays and finally playing with the rules. Here it is and yay, it worked:

rule "Ceiling_fan_ON"
when
    Item Xiaomi_Sensor_LR_Temp received update
then
    if (Xiaomi_Sensor_LR_Temp.state >= 24){
        if (Xiaomi_Switch_Ceiling.state != ON) Xiaomi_Switch_Ceiling.sendCommand(ON)
    }
    end

rule "Ceiling_fan_OFF"
when
    Item Xiaomi_Sensor_LR_Temp received update
then
    if (Xiaomi_Sensor_LR_Temp.state <= 23){
        if (Xiaomi_Switch_Ceilingstate != OFF) Xiaomi_Switch_Ceiling.sendCommand(OFF)
    }
    end

I used the earlier rule because I couldn’t work out exactly how to create it using the var fanstate you had recommended when it came to sending MQTT commands, but, it works at least so I’m happy :slight_smile:

The rules have the same trigger so you could combine them in one:

rule "Ceiling fan"
when
    Item Xiaomi_Sensor_LR_Temp received update
then
    if (Xiaomi_Sensor_LR_Temp.state >= 24){
        if (Xiaomi_Switch_Ceiling.state != ON) Xiaomi_Switch_Ceiling.sendCommand(ON)
    }
    if (Xiaomi_Sensor_LR_Temp.state <= 23){
        if (Xiaomi_Switch_Ceilingstate != OFF) Xiaomi_Switch_Ceiling.sendCommand(OFF)
    }
end