[SOLVED] Channel select with broadlink and mqtt


(Noam ) #1

hello

i have this in my setup

item

Number Channel_select “Channel select”

sitemap

Selection item=Channel_select mappings=[001=“MOVIES 1”, 002=“MOVIES 2”, 003=“MOVIES 3”, 004=“MOVIES 4”…

rule

rule “Tvswitcher”
when
Item Channel_select changed
then
if (Channel_select.state == “NULL”) return; // If NULL do nothing
val topic = “broadlink/tvswicher/select/tv” + Channel_select.state
publish(“mosquitto”, topic, “play-0d43b4e42518”)
end

when i select one of the channels, i get this (for example channel 001)

broadlink/tvswicher/select/tv001

and i want it to send 3 command with the first, second and third number, like this

broadlink/tvswicher/select/tv0
wait 1sec
broadlink/tvswicher/select/tv0
wait 1sec
broadlink/tvswicher/select/tv1

how can i do it?

thanks


(Vincent Regaud) #2

First things first, please use the code fences when publishing code, thanks

There are several solutions to your problem.

  • Solution 1: Use timers (Recommended)
var topic = "" //AT THE TOP OF THE RULES FILE!!

rule “Tvswitcher”
when
    Item Channel_select changed
then
    if (Channel_select.state == “NULL”) return; // If NULL do nothing
    topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(0, 1)
    publish(“mosquitto”, topic, “play-0d43b4e42518”)
    createTimer(now.plusSeconds(1), [ |
        topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(1, 2)
        publish(“mosquitto”, topic, “play-0d43b4e42518”)
    |]
    createTimer(now.plusSeconds(2), [ |
        topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(2)
        publish(“mosquitto”, topic, “play-0d43b4e42518”)
    |]
end

If the string was longer we would use a loop through the string an increment the substring indexes when looping but as you only have 3 characters this should be enough and writing the loop will not save lines of code

When the rule executes it will send the find message straight away and create 2 timers to be executed 1 seconds after and 2 seconds after with the respective topics. Because the timers are execute outside the rule, we need to move the declaration of the topic variable to the top of the rules file and that it’s scope becomes global and can be accessed by the timers.

  • Solution 2: Use Thread::sleep (Not recommended)
rule “Tvswitcher”
when
    Item Channel_select changed
then
    if (Channel_select.state == “NULL”) return; // If NULL do nothing
    var topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(0, 1)
    publish(“mosquitto”, topic, “play-0d43b4e42518”)
    Thread::sleep(1)
    topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(1, 2)
    publish(“mosquitto”, topic, “play-0d43b4e42518”)
    Thread::sleep(1)
    topic = “broadlink/tvswicher/select/tv” + Channel_select.state.toString.substring(2)
    publish(“mosquitto”, topic, “play-0d43b4e42518”)
end

(Psyciknz) #3

I think I do similar. Are you using broadlink-MQTT service. It has macros. Ie multiple commands

Will post sitemap/items tomorrow


(Noam ) #4

thanks you, this is what i was looking for “toString.substring(0, 1)”

working now


(Noam ) #5

i’m using this

it as gui for recording command and i dont think it support macros


(Psyciknz) #6

I use this one: https://github.com/eschava/broadlink-mqtt

Then my items file

String rm3_dining_stereo_radio "Source" (g_rm3_lounge_samsung) {mqtt=">[openhab:broadlink/34ea34b2fa7f/receiver/yamaha/1:command:zm:replay],>[openhab:broadlink/34ea34b2fa7f/receiver/yamaha/4:command:sound:replay],>[openhab:broadlink/34ea34b2fa7f/receiver/yamaha/2:command:edge:replay]"}

And sitemap

Selection item=rm3_dining_stereo_radio mappings=[zm=ZM,edge="The Edge",sound=Sound]

(Noam ) #7

i ask the devloper to add macros, dont know if he will, but i love the gui where i can record and see all commands list, also can check fast and easy if i have problem with my commands


(Psyciknz) #8

THat one above, the commands and macros are recorded as files, which makes them transferable…and with some conversion you can convert them to other broadlink formats (ie the broadlink binding)