Nest State item to boolean or graphable item

Hey everyone, obviously new to this, hoping to seek some help.
I’ll do my best to get the first post right. So let’s start with what I am trying to do.

I have a nest binding (OH2) that has an item ( LivingRoomThermostat_State ) when it’s in my homesite it reads when the HVAC unit is in a “cooling”, “heating”, or “off” mode. No switch or number or anything, pure string value.

What I want to do is be able to graph (I have Grafana and InfluxDB as my persistence ) when and how long the A/C unit is in cooling mode. I can see (LivingRoomThermostat_State) in Grafana as a item. But no data is available.

My assumption(correctly me if i am wrong) is the string(text) should be converted out to a number state.

So here is my sorry excuse for trying to attempt this:

rule "Thermostat living room to number"
        when

         Item LivingRoomThermostat_State changed

        then
        if(LivingRoomThermostat_State.state!="cooling")
        {
                LRthermostate_Number.sendCommand(1)
        }
        else
        {
                if(LivingRoomThermostat_State.state!="off")
                {
                        LRthermostate_Number.sendCommand(0)

                                }

        }


 end

It…kinda works. no… no it doesn’t really work. No matter what going [‘off’ to ‘cooling’] or [‘cooling’ to ‘off’] it populates item LRthermostate_Number with a value of 1.

Regardless of that, I feel like there is an easier way to get this string into Grafana without rules. I just need a little bit of direction with this. OR a way to simply convert this string into an usable item.

Thanks for any help or direction.

-Chase

First off: I don’t know graphana and Nest, but your code won’t get all the states you’ll want. You have three states, the LivingRoomThermostat_State can take: ON, HEATING and COOLING?
Your goal is to graph 0 for OFF, 1 for HEATING and 2 for COOLING?

please post your Definition of both items: LivingRoomThermostat_State and LRthermostate_Number and your logs (events.log and openhab.log) after saving the rule and after triggering the Nest’s state. Then we can have a look on these.

For the assumed goal, you’ll need a rule like:

rule "Thermostat living room to number"
when
    Item LivingRoomThermostat_State changed
then
    if (LivingRoomThermostat_State.state === "cooling") {
            LRthermostate_Number.sendCommand(2)
    }
    if (LivingRoomThermostat_State.state === "heating") {
            LRthermostate_Number.sendCommand(1)
    }
    if (LivingRoomThermostat_State.state === "off") {
            LRthermostate_Number.sendCommand(0)
    }
 end

PS - the rules-engine runs on XTEND, so:

  • please have a look on if-clauses for proper use. :wink:
  • you could also use switch for a slightly smoother reading of the code of course.

If you’re going to only use InfluxDB for this, then as you’ve found, you need a numerical value to persist to an item.

Once you’ve done this, you can use the Discrete plugin for Grafana, and then this will give you a nice time-line style graph with your various states, like I’m using here to show Presence for the 4 people in the house:


(I only reconfigured my openHAB installation this morning, hence only a small amount of data so far)

I’m using MySQL persistence, so the metrics setup will be slightly different to using InfluxDB:

SELECT
FLOOR(UNIX_TIMESTAMP(time)) as time_sec,
CASE WHEN value = 'ON' THEN 1 ELSE 0 END as value,
'Confused' as metric
FROM openhab.item0001
WHERE $__timeFilter(time)
ORDER BY time ASC

You will then set up Mappings to convert your numerical values into words again:

image

You can also set up Color mappings to customise what colours you have - maybe you want grey (off), blue (cooling) and red (heating)?

Alternatively, you could set up an additional Persistence for non-numeric items (such as MySQL), and then you don’t need any additional items or rules, and you can configure the Discrete plugin to look directly at the values stored against the Nest State item.

1 Like

Do you know if you can mix a discrete chart like this with a line graph? That might be a better way for me to show when my fan/heater is running compared to the various temp sensors.

I don’t believe Grafana allows you to overlay different types of panels :frowning: You could set the two up underneath other, but the left/right doesn’t match up due to axis labels & different margins etc.

The only other option I can think of is to use a secondary Y axis on the standard graph panel with 0 & 1 as the values, and give the switch item series a background colour and enable fill/shading, and enable stepped mode on these series so that you get the on/off effect.

As soon as I reconfigure MQTT so I’m getting some temperature sensor values, I may give that a try if you’ve not got round to it yet.

This is what I’ve come up with so far.

image

As I’m only persisting everyChange for the On/Off item, the line doesn’t extend to the right from the last known value. If I saved it every minute instead, it probably would.

That is essentially what I do now.

The blue stair step at the bottom shows when the fan is running. It is red when the heater is running.

Thanks for helping. Garry, I really like that method of showing the sting in that style of graph. For sure going to try that once I get the rule set up.

Thomas, Thanks for the new rule type up. I had purposely left out the heating just to keep the rule simple, its so blazing hot here I wouldn’t even be able to test and trigger that state.
I went ahead and copied in your rule, just to give it a try. Of course I brushed up on my operators. Yours does make a lot more sense, it just won’t fire.
No errors when i save, its happy about it, all other rules do fire correctly.

Here are the two items i created (oh FYI I do have an upstairs and downstairs thermostat and the same set up for both. I’m just tackle the downstairs section first and then i’ll copy it and change the names for upstairs).

//Thermostat change over

Number LRthermostate_Number "Downstairs"

Number  USthermostate_Number "Upstairs"

Since the NEST binding is automatic it is stored in

/var/lib/openhab2/jsondb

The item is in org.eclipse.smarthome.core.items.Item.json
Here is the auto created item for state:

  "LivingRoomThermostat_State": {
    "class": "org.eclipse.smarthome.core.items.ManagedItemProvider$PersistedItem",
    "value": {
      "groupNames": [],
      "itemType": "String",
      "label": "State"
}
}

Here is a screenshot of the log, at the top there the rule file did save correctly. This is with the old rules fyi, you can see it shifts and logs a 1.
But when it shuts off, it logs 1 again.

So scratch that, I edited the rules to the above code and here is the screenshot for that (here both upstairs and downstairs switched states within a second) Both have the same code, just differences in item to populate the new value to.

Now here is one going from OFF to COOLING on the living room Nest.

This seems to work actually.
Changes:
triple equals to double equals. Perhaps the value between the two are not the same?
Used full caps just like the log shows.
changed quote style to single quote. Now, i’m not sure if this did anything, i just read about that in the Xtend rules on strings improving S/N ratio. Maybe you guys can explain that better.

But this code is working.
I’ll update the persistence to pull these values every minute to show a better graph.

rule "Thermostat living room to number"
        when
                 Item LivingRoomThermostat_State changed
        then

    if (LivingRoomThermostat_State.state == 'COOLING')
{
            LRthermostate_Number.sendCommand(2)
    }
    if (LivingRoomThermostat_State.state == 'HEATING')
 {
            LRthermostate_Number.sendCommand(1)
    }
    if (LivingRoomThermostat_State.state == 'OFF')
{
            LRthermostate_Number.sendCommand(0)
    }
 end

Only use triple equals when one of the operands is null. In all other cases you should always use double equals.

I know this is an old post but I am going through with the same problem and I’d like to know what is LRthermostate_Number is it Item type Number and that is it? How it is being used? If I can get some more details then it will be helpful.