Jump to content
🌟 NOTIFICATION/Benachrichtigung: Welcome to our New Store! - shelly.com 🌟 ×

simulation of virtual components oin gen2 devices


Recommended Posts

  • Members
Posted (edited)

with such a script you can emulate virtual components on the MQTT interface of a gen 2 device:

const mqttPrefix = Shelly.getComponentConfig( "mqtt" ).topic_prefix;

let value = "off";

function showState()
{
    print( "send", value );
    MQTT.publish( mqttPrefix+"/status/enum:200", "{\"value\":\""+value+"\"}", 1, false );
}

MQTT.subscribe( mqttPrefix+"/rpc", 
    function(topic,message,ud)
    {
        //print(message)
        message = JSON.parse( message );
        if( message.method === "Enum.Set" && message.params.id == 200 )
        {
            let answer = {
                id:     message.id,
                src:    mqttPrefix,
                dst:    message.src,
                result: {}
            };
            print("set",message.params.value);
            value = message.params.value;
            MQTT.publish( message.src+"/rpc", JSON.stringify( answer ), 1, false );
            showState();
        }
    }
    , null
);

showState();
print("started",mqttPrefix)

the only drawback is, that you get two answers:

  1. error message from Shelly firmware
  2. result message from the script

so the master SW has to deal with this.

Edited by mschaeffler
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Erstelle neue...