Create a script based switch channel

What i have:

  • A Bravia TV
  • Unifi binding

Issue:

  • I would like to have an Equipment that is OFF when screen is off (standby) and ON if on
  • TV Is connected to wifi even in Wifi so cant use that channel

Assuming i can query via IP the TV, can i use JS/whatever to create a script and use that to change status of a switch item that can be associated with the TV equipment?

P.S. i tried both the Bravia and the Android TV binding with no success, however i can just check port 8443, if its responding then the TV is on, if not TV is in standby

Maybe you can use the network binding and ping the tv.

I’m not sure I understand the question. Are you asking here if you could use a rule to sync a Switch (controls equipment) with another Switch (TV status)?

If yes than the answer is yes. You might also be able to use the follow profile for this.

@deibich probably has the best solution for detecting the status of the TV if the other bindings do not work.

In the rule approach have a rule that triggers when the TV status Item (let’s call it TV_Status) changes state and command the Equipment based on that state. If you split it into two rules you don’t really even need any programming language and it can be done all in the basic UI rules. One triggers when the status Item changes to ON and sends command to the equipment Switch ON and the other for OFF.

In the code tab it will look a little bit like this only simpler. In this example I have a condition but you could use a trigger of “changed to ON” and “changed to OFF” instead of the condition I had to use because it’s a number.

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: vCloudiness
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "2"
    label: vCloudiness > 50 %
    configuration:
      itemName: vCloudiness
      state: 50 %
      operator: ">"
    type: core.ItemStateCondition
  - inputs: {}
    id: "4"
    configuration:
      itemName: vIsCloudy
      state: ON
      operator: "!="
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: ON
      itemName: vIsCloudy
    type: core.ItemCommandAction
configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: vCloudiness
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "4"
    configuration:
      itemName: vIsCloudy
      state: OFF
      operator: "!="
    type: core.ItemStateCondition
  - inputs: {}
    id: "5"
    configuration:
      itemName: vCloudiness
      state: 50 %
      operator: <
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: OFF
      itemName: vIsCloudy
    type: core.ItemCommandAction

Note, if you go this approach you can create one rule, click the “Code” tab, copy the YAML you find there, create a new rule, click the “Code” tab and paste. Edit the ONs to OFF.

In a text based JS Scripting rule using the rule builder it would look something like (I’m being thorough and testing for NULL and UNDEF which will generate errors if attempting to command an Item with them.

rules.when().item('TV_Status').changed()
  .if( event => {
    return !item.TV_Status.isUninitialized
  }
  .then( event => { 
    if(!items.TV_Status.isUninitialized) {
      items.Equipment_Control.sendCommand(event.newItemState);
    }
    // do nothing if TV_Status changed to NULL or UNDEF
  }).build('TV Control');

Or to follow the two rule approach which is simpler but uses two rules.

rules.when().item('TV_Status').changed().toOn().then(sendOn().toItem('EquipmentControl')).build("TV Control ON");
rules.when().item('TV_Status').changed().toOff().then(sendOff().toItem('EquipmentControl')).build("TV Control OFF");

I don’t use profiles too much, but I think you would create the Item for the TV_Status and link the Network Binding Channel to that Switch. On the link, set the Follow profile and choose the Channel that controls the equipment. Then when the Network Binding Channel sends an ON update to the Item, that will be forwarded as a command to the equipment Channel.

One draw back to this is that it’s only one way (changes from the Equipment do not get forwarded to the Network Binding) but that’s OK in this case I think.

1 Like

Thanks for your reply!

No, or better not until I saw the network binding, i already have a way to send on / off commands via IR using a binding i made (for a Switchbot Hub2) problem is that just sends a command and won’t tell me weather the TV is actually on or off, the only way is to monitor an http port, network binding can do that via service device so i guess i am good.

Still not sure I understood how i would have done that without the binding by using scripting only.

Generally:

  • Items represent an actuator or controller on a device
  • Things represent a device or API
  • Channels represent an actuator or controller on a device
  • Links connect Items to Channels
  • Profiles intercept the data going from the Channel to the Item and/or from the Item going to the Channel and manipulates it in some way
  • Rules orchestrate behaviors based on events by manipulating the states of Items. There is also some ability to do other stuff too but generally their purpose is to send commands to Items based on events and current states of other Items/date time/etc.

Detecting if a TV is on is really more of a sensor so if it’s an option to implement that in a binding, that’s where that capability belongs. It’s the best place for it and it will work the most smoothly.

However, that is not always a possibility so you could write a rule, but it’s always going to be a little bit awkward in comparison. You have rule Actions to send various HTTP requests (GET, PUT, POST) or running shell scripts and commands and there is a transform Action to parse and extract data from the results which can then be used to command or update an Item.

So one way could have been to have a rule with a cron trigger to use executeCommandLine to run ping or wget or curl and parse the output to see if the TV was on. But that’s a pain and sort of the whole reason we have bindings like Network and Exec. For the most part, you will always be better off working with existing bindings or implementing this into your own binding if you are writing one. But not everyone can write bindings so sometimes we do what we must in rules. (Note, I’ve even implemented full support for Honeywell’s now renamed Residio API using only OH rules to include OAuth2, but it was always brittle and I gave up and haven’t gone back to implement it as a binding yet).

There is some support for generic third party libraries though because it’s running on the JVM instead of a standard Node.js server not all third party libraries you might find on npm are guaranteed to work. I think jRuby has a little better support for gems. HABApp runs completely outside of OH in it’s own Python process so of course it has full support for pip.

Sometimes, it’s handy to just have something completely outside of OH itself that does all the integration work and reports sensor readings and respond to commands through some sort of standardized communication mechanism. For example, maybe the location of your OH server isn’t ideal and you have a spare ESP32 or RaspberryPi 0W hanging around that you can put closer. MQTT is usually chosen because it’s almost universally supported by home automation hubs and it’s a pretty good protocol in this space.

I myself have sensorReporter which can talk MQTT or openHAB’s REST API but there are lots of others including mqtt2any, zigbee2mqtt, WavePlus_Bridge, etc. If one wants to support more than one hub or rely on third party libraries that make it difficult to integrate with the device’s API without and they won’t work in OH Rules, and you can’t or don’t want to write an OH add-on, this is a common approach.

Ultimately, your primary goal is to get the status of the TV into an Item. And there are lots of ways you can achieve that but often rules is not the best choice. But rules are turning complete so they could support interacting with just about anything.

Nice, yeah basically exec and network binding are replacing my use case 99% of times still useful to know this