Help Needed - Habpanel

I want to start by apologizing for possibly posting under the wrong section, and using all the wrong terms on my following request. With English as a second language is hard for me to find online what I’m looking for due to using the wrong terminology. With that being said, I’ve managed to set up OpenHab successfully, mosquito MQTT client, Habpanel; very simple, very basic, switch-driven Habpanel UI; and I’ve been using it for some time now.

I’m not good at scripting or using any programming language, so I tend to copy and paste a lot, and edit things to my needs.

Brace yourselves, and forgive me for using the wrong terms next:
I have buttons on my habpanel that send httpPost requests, this communicates with my RM Pro (an infrared device) which runs a macro that turns on my tv, receiver, inputs, android box, etc. When clicked the button sends an ON command, and when clicked again an OFF command, which turns all these devices on or off, very basic, very simple. The TV has wifi, and I can bind the ip and see it in OpenHab, so I know when the TV is online or not. Using this I want the button to send an ON command when TV is ‘offline’, and an OFF command when the TV is ‘online’. Why? There is a series of buttons that must be “pressed” in the macro to get the TV to the right input, and they’re different when turning all the devices ON and when turning them OFF, so while the TV is ON, and I’m meaning to turn everything OFF, I don’t want the button to send the ON command. Everything has to be triggered by a click (ngclick type of script?)

Basically I’m looking for something like this:

If TV is 'off’
off = (TV’s ip bind that shows when it’s online or offline)
then if ngclick httpPost (http link here ON)
else
ngclick httpPost (http link here OFF)

in other words: if TV’s wifi is offline then when button is clicked it’ll send the httppost link that turns everything ON, else if the TV is online then if the button is clicked it should send the off httppost link.

Right now I’m managing with two separate buttons, one for OFF, and one for ON. I need one button that can tell when the TV is on or not and send the correct command accordingly (only when clicked)

Guys I hope some of above made sense and that you are able to help and/or provide a sample script that I can use in a template within habpanel. Thank you so much in advance!

EDIT: this is how the two buttons work now. I want it to be one button that checks the TV state and chooses the right command:

<button style="width: 100%; height: 1em;
border: 0; color: green; background: transparent;
font-size: 16px" ng-click="sendCmd('Movie_Mode_On', 'ON')">
<img src="https://home.myopenhab.org/icon/switch?format=svg" style="width:20px;height:20px;"></img>
</button>
<br />
<br />

<button style="width: 100%; height: 1em;
border: 0; color: red; background: transparent;
font-size: 16px" ng-click="sendCmd('Movie_Mode_Off', 'ON')">
<i class="glyphicon glyphicon-off"></i>
</button>
<br />

No problem. I moved it to where it will get more attention from the right people.

I recommend you:

  • create a Switch Item to represent the TV’s state
  • update your HABPanel with this Switch, put it on HABPanel as a Switch instead of a Button
  • create a Rule that triggers when the Switch receives a command something along the lines of:
rule "TV Control"
when
    Item Movie_Mode received command
then
    if(receivedCommand == ON) {
        // do ON stuff
    }
    else {
        // do OFF stuff
    }
end

Thanks for your prompt reply. My apologies if I didn’t understand your suggestion. How does the switch know which command to send? Where in the rule does it know the TV wifi state is on or off?

The switch needs to know when the TV is connected to the wifi so that when I toggle the switch it knows if to run movie mode on or movie mode off commands. In other words it needs to check the state of the TV’s wifi to decide

I suggest you read the Beginner’s Tutorial and play around with the Demo.

You need Items to represent the state of your TV (Network Binding will probably work if the TV goes off the network when it is OFF) and to send your commands to your device.

From what you posted it looks like you have your device already linked to an Item named Movie_Mode_On and Movie_Mode_Off so what ever you do when those buttons get pressed needs to be done in the rule that gets triggered by the switch.

I’m sorry if I didn’t explain things correctly. I don’t want the TV’s wifi state to trigger these commands. I want a switch or button to check whether the TV is ON by checking its wifi state and then send the proper command (on or off).
Like:

I press the button on habpanel.
Button checks if tv is on (by the wifi state).
If the TV is on
It sends one command
If the TV is off
It sends a different command

I need to translate that into a script I can paste into a widget template in habpanel, or a rule that I can pick from when I add a button or switch widget. Does that make sense?

So Movie_mode_on turns on TV, and Android box, and switches input to Hdmi 1

Movie_mode_off turns off box, switches input back to regular channels, then turns off the TV.

For either of these commands to be sent I want the button or switch to check the TV state first using the TV’s wifi state.

i agree with rich. in the rule, if the state is on, then send command off, if state is off, send commanf on.

Right.

Like I said, you need an Item to represent the wifi state of the TV, probably linked to the Network binding.

When you press the button in HABPanel it triggers a rule that checks the state of that Item and sends the appropriate commands.

This is why I recommend reading the Beginner’s Tutorial and some of the User’s Guide as well.

None of this belongs as a template in HABPanel.

Item receives a command. Command triggers a Rule. Rule does stuff. The only thing you do in HABPanels is put a Button or Switch Widget linked to a specific Item.

I think you lack a grasp of some of the fundamental concepts about how OH works which the Beginner’s Tutorial, User’s Guide, and playing around with the Demo should help to enlighten you.

Ok here is what i have so far:

on my sitemap i have:

Switch item=sTV_state

on my items i have:

Switch sTV_state { channel=“network:device:192_168_1_23:online” }

on rules:

rule "Samsung TV Rule"
    when
        Item sTV_estate received command
    then
        if(receivedCommand == ON) {
            sendCommand(Tv_Mov_Off, ON)
            
		}
		else {
			sendCommand(Tv_Mov_On, ON)
		}
end

and it’s not working. What am I missing?

You misspelled the name of sTV_state in your rule trigger.

But do you really want to trigger the rule when the TV goes offline?

I guess I’ll say it for a third time.

You need a Switch that you put on your sitemap and that you use to trigger the rule.

You need a different Switch that represents the state of your TV.

You need yet another Item that you send commands to to control your IR system.

Thanks, i completely missed that. The idea is that if the TV is ‘online’ the rule tells the switch to send the command to run the _Off macro, and when the TV is ‘offline’ it will send the command that runs the _On macro. I think i had to also define the tv state in conf/things, so i did that, fixed the rule and it’s all working!

I thank you so much Rik, you’re a very helpful person.