HABApp - Get Group State

Hey @Spaceman_Spiff,

Still trying to get started with some simple rules.

I’m trying to get the ON/OFF state of a group. The API Explorer returns this:

{
  "members": [
    {
      "link": "http://pi-openhab:8080/rest/items/g3ChronosOffWhenBright",
      "state": "100",
      "type": "Dimmer",
      "name": "V_Lights_KitchenSink",
      "label": "Sink Lights",
      "category": "slider",
      "tags": [
        "Control",
        "Light"
      ],
      "groupNames": [
        "g3ChronosOffWhenSleep",
        "g3ChronosOffWhenBright"
      ]
    }
  ],
  "groupType": "Switch",
  "function": {
    "name": "OR",
    "params": [
      "ON",
      "OFF"
    ]
  },
  "link": "http://pi-openhab:8080/rest/items/g3ChronosOffWhenBright",
  "state": "ON",
  "editable": true,
  "type": "Group",
  "name": "g3ChronosOffWhenBright",
  "label": "Bright - Off",
  "category": "sun",
  "tags": [
    "Switch"
  ],
  "groupNames": []
}

and in a HABAPP rule these commands:

        group_item = GroupItem.get_item("g3ChronosOffWhenBright")
        log.warning(group_item)

return:

<GroupItem name: g3ChronosOffWhenBright, value: None, last_change: 2021-04-10 17:44:30.610776, last_update: 2021-04-10 17:44:30.610776>

Maybe I’m missing something (wouldn’t be the first time) but it appears that it is not returning the group state?

Do you see the corresponding event in the HABApp log if you flip one of your switches?

Also take a look at the MultiModeItem. It allows to easily build logic without having to use groups.

A restart of the service resolved the problem. I can’t remember if I restarted it since I created that group. Thanks for the tips. I’ll look into MMI anyway.

@Spaceman_Spiff - Well, I spoke too soon.

I am no longer getting NULL values, but the value doesn’t change when I toggle g3ChronosOffWhenBright manually in MainUI. here is my whole code (apologies if code fence is wrong):

import HABApp
from HABApp.core.events import ValueChangeEvent
from HABApp.openhab.items import NumberItem, GroupItem
import logging


class MyRule(HABApp.Rule):
    def __init__(self):
        super().__init__()
        self.listen_event('V_HABApp_Ping', self.on_change, ValueChangeEvent)

    def on_change(self, event: ValueChangeEvent):
        sensor_item = NumberItem.get_item('Hue_SensorLightOutdoor_Illuminance')
        group_item = GroupItem.get_item("g3ChronosOffWhenBright")

        if sensor_item.get_value() > 100:
            log.warning(str(group_item.get_value()))
            if str(group_item.get_value()) == "ON":
                log.warning('Chronos - Off When Light')
                group_item.oh_send_command("OFF")
        else:
            if str(group_item.get_value()) == "OFF":
                log.warning('Chronos - On When Dark')
                group_item.oh_send_command("ON")


log = logging.getLogger('MyRule')
MyRule()

You are listening to V_HABApp_Ping instead of your group item.
Also you can use group_item.value instead of get_value(default=99) which exists to provide a default if the value is None

I was just using V_HABAPP_Ping as a proxy for the item that I wanted to use, as it updates every 20 seconds.

I am not trying to detect changes to the group item directly. Just if another item changes, check the status of the group item, and if it is ON turn it OFF.

I’ll do a few more tests.

@Spaceman_Spiff Nope. I’m either an idiot or there is something wrong. Likely the former. But I’ve simplified the code for testing purposes down to:

from HABApp.openhab.items import GroupItem
import logging

log = logging.getLogger('MyRule')
group_item = GroupItem.get_item("g3ChronosOffWhenBright")
log.warning(group_item.value)
log.warning(group_item.get_value())
group_item.oh_send_command("ON")
log.warning(group_item.value)
log.warning(group_item.get_value())
group_item.oh_send_command("OFF")
log.warning(group_item.value)
log.warning(group_item.get_value())

I even added some sleep commands between the send_command and logging the value. In all cases I only read OFF:

I can probably work around it… but just trying to get the basics working before migrating rules.

You need larger delays, try 100 ms.
Please also take a look at the HABApp_events.log. There you see all changes that get read from openhab and you should easily see the values change.
Also check the “read_only” config switch.

Last but not least check openhab.log for errors

I see them changing in HABApp_events, and in openhab.log. I had set the sleep interval between changing the state to 2s and 1s before the last post (it complained about a long running script both times). I just tried with .1s and same result (as expected).

[2021-04-11 13:17:07,638] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <GroupItemStateChangedEvent name: g3ChronosOffWhenBright, value: OFF, old_value: ON>
[2021-04-11 13:17:55,805] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/command', 'payload': '{"type":"OnOff","value":"ON"}', 'type': 'ItemCommandEvent'}
[2021-04-11 13:17:55,805] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <ItemCommandEvent name: g3ChronosOffWhenBright, value: ON>
[2021-04-11 13:17:55,823] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/V_Lights_KitchenSink/statechanged', 'payload': '{"type":"OnOff","value":"ON","oldType":"OnOff","oldValue":"OFF"}', 'type': 'GroupItemStateChangedEvent'}
[2021-04-11 13:17:55,825] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <GroupItemStateChangedEvent name: g3ChronosOffWhenBright, value: ON, old_value: OFF>
[2021-04-11 13:18:38,205] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/command', 'payload': '{"type":"OnOff","value":"OFF"}', 'type': 'ItemCommandEvent'}
[2021-04-11 13:18:38,205] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <ItemCommandEvent name: g3ChronosOffWhenBright, value: OFF>
[2021-04-11 13:18:38,224] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/V_Lights_KitchenSink/statechanged', 'payload': '{"type":"OnOff","value":"OFF","oldType":"OnOff","oldValue":"ON"}', 'type': 'GroupItemStateChangedEvent'}
[2021-04-11 13:18:38,224] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <GroupItemStateChangedEvent name: g3ChronosOffWhenBright, value: OFF, old_value: ON>
[2021-04-11 13:18:38,286] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/command', 'payload': '{"type":"OnOff","value":"ON"}', 'type': 'ItemCommandEvent'}
[2021-04-11 13:18:38,286] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <ItemCommandEvent name: g3ChronosOffWhenBright, value: ON>
[2021-04-11 13:18:38,339] [  HABApp.EventBus.openhab]    DEBUG | {'topic': 'openhab/items/g3ChronosOffWhenBright/V_Lights_KitchenSink/statechanged', 'payload': '{"type":"OnOff","value":"ON","oldType":"OnOff","oldValue":"OFF"}', 'type': 'GroupItemStateChangedEvent'}
[2021-04-11 13:18:38,339] [          HABApp.EventBus]     INFO | g3ChronosOffWhenBright: <GroupItemStateChangedEvent name: g3ChronosOffWhenBright, value: ON, old_value: OFF>

I can just never successfully read the group value or get_value(). It always comes back as OFF.

Further simplifying… this code logs OFF whether the group specified is ON or OFF.

from HABApp.openhab.items import GroupItem
import logging

log = logging.getLogger('MyRule')
group_item = GroupItem.get_item("g3ChronosOffWhenBright")
log.warning(group_item.get_value())
log.warning(group_item.value)

I’ll investigate - you’re doing everything right.

Cool. Please let me know if there is anything I can do to help.

here is the item definition I created for testing purposes if it helps.

{
  "members": [
    {
      "link": "http://pi-openhab:8080/rest/items/g3ChronosOffWhenBright",
      "state": "100",
      "type": "Dimmer",
      "name": "V_Lights_KitchenSink",
      "label": "Sink Lights",
      "category": "slider",
      "tags": [
        "Control",
        "Light"
      ],
      "groupNames": [
        "g3ChronosOffWhenSleep",
        "g3ChronosOffWhenBright"
      ]
    }
  ],
  "groupType": "Switch",
  "function": {
    "name": "OR",
    "params": [
      "ON",
      "OFF"
    ]
  },
  "link": "http://pi-openhab:8080/rest/items/g3ChronosOffWhenBright",
  "state": "ON",
  "editable": true,
  "type": "Group",
  "name": "g3ChronosOffWhenBright",
  "label": "Bright - Off",
  "category": "sun",
  "tags": [
    "Switch"
  ],
  "groupNames": []
}

I’ve just released 0.30.0 which fixes the issue.