Could not cast 7.7 to ...DecimalType in rule

I have to admit this error doesn’t make much sense to me.

Do you see DWD_Vorhersage_d_Sonnenstunden_0 successfully updating in the events.log?

Does it appear on your sitemap correctly (i.e. doesn’t have “-” for the value)?

Assuming there is nothing wrong with the Item state, you don’t need to call doubleValue, particularly if you are storing that in a Number. The state of a Number Item (i.e. DecimalType) is already a Number so you only need

val Number valVorhersage=DWD_Vorhersage_d_Sonnenstunden_0.state as Number

If there were a problem with the number returned by your script, it would fail long before this Rule ever runs because you would see something like “Could not parse 7.7 to a Number” or something like that and the Number Item would go to NULL (I think).

But it is failing in your Rule. The only two possible states a Number Item can take is a valid Number or NULL. It is is a valid Number, it can’t matter what the number is because if there were something wrong with it it would have failed before the Rule ran and the “7.7” you are seeing in your openhab.log wouldn’t be there.

Rich, thank you for your reply. I checked die Log again after updating the values. This is what I see:

2018-08-20 19:08:20.371 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_Sonnenstunden_0 changed from 10.0 to 10.3
2018-08-20 19:08:20.381 [INFO ] [home.model.script.astro_wetter.rules] - SonnenstundenProzent Tag 0 berechnen
2018-08-20 19:08:20.402 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_0 changed from 71.77033495 to 73.92344499
2018-08-20 19:08:20.741 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_Sonnenstunden_1 changed from 11.7 to 10.8
2018-08-20 19:08:20.755 [INFO ] [home.model.script.astro_wetter.rules] - SonnenstundenProzent Tag 1 berechnen
2018-08-20 19:08:20.785 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_1 changed from 83.97129189 to 77.51196174
2018-08-20 19:08:21.148 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_Sonnenstunden_2 changed from 10.0 to 8.9
2018-08-20 19:08:21.143 [INFO ] [home.model.script.astro_wetter.rules] - SonnenstundenProzent Tag 2 berechnen
2018-08-20 19:08:21.161 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'SonnenstundenProzent Tag 2 berechnen': Could not cast 8.9 to org.eclipse.smarthome.core.library.types.DecimalType; line 56, column 28, length 53
2018-08-20 19:08:21.641 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_Sonnenstunden_3 changed from 7.7 to 7.0
2018-08-20 19:08:21.650 [INFO ] [home.model.script.astro_wetter.rules] - SonnenstundenProzent Tag 3 berechnen
2018-08-20 19:08:21.663 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'SonnenstundenProzent Tag 3 berechnen': Could not cast 7.0 to org.eclipse.smarthome.core.library.types.DecimalType; line 72, column 28, length 53
...

Interesting is day 2, the value changed from 10.0 to 8.9 => with 10.0 the rule worked, now I get an error.
Sitemap looks OK:
image
So I still don’t understand that error :confused:

But - that’s not a problem at the moment, because your hint to use “…state as Number” solved my problem!

2018-08-20 20:15:55.507 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_4 changed from 100 to 20.81339713
2018-08-20 20:15:55.525 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_0 changed from 100 to 73.92344499
2018-08-20 20:15:55.529 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_3 changed from 100 to 50.23923446
2018-08-20 20:15:55.533 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_1 changed from 100 to 77.51196174
2018-08-20 20:15:55.536 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_2 changed from 100 to 63.87559810
2018-08-20 20:15:55.538 [vent.ItemStateChangedEvent] - DWD_Vorhersage_d_SonnenstundenProzent_5 changed from 100 to 15.78947369

Thank you very much!

Very odd. It makes no sense that that would fix it but it did work so I guess we can just be happy and move on. :slight_smile:

Question regarding the OP and this post (by you :wink:)

https://community.openhab.org/t/as-number-vs-as-decimaltype/22209/2?u=h102

Could the OP’s rule have failed due to an object having (or seeing, for lack of words) two states and the OH rule being confused? Still trying to wrap my head around class hierarchy and how it works.

No. An Item can only have one State at a time. It is certainly possible that an Item could change state while a Rule is running and that can cause some problems when people do not plan for that or expect it. It can also take some time for an Item to reach a state after it has been commanded or updated which causes some people problems (e.g. sendCommand to an Item then immediately log our the state of that Item and see in the log that the Item is still in the old state because the Item hasn’t finished processing the command yet).

But none of those cases are present in OP’s Rule and the error he saw had to do with type.

The reason it was confusing was because it says “Could not cast 7.7 to DecimalType” meaning that the 7.7 is getting to the Rule somehow which should eliminate the possibility that the Item is NULL. And a number Item can only have a DecimalType or NULL as its state so the error really doesn’t make sense. Something else weird is going on, perhaps having to do with the calls to doubleValue, but there again I would have expected the error to be different.

Now the problem that I discussed in that thread you linked to is different. In Object Oriented Programming, There are two relationships: “is a” and “has a”. What that post is showing the “is a” relationships for the DecimalType class. The tl;dr is that DecimalType is also a Number. The one Object (instance of a Class) is both types at the same time.

Given that, let’s say we have the following sendCommand methods on the NumberItem class:

  • sendCommand(String cmd)
  • sendCommand(DecimalType cmd)
  • sendCommand(Number cmd)

Now lets say I call sendCommand with a DecimalType. The Rules Engine gets confused because DecimalType is both a DecimalType and a Number. Consequently two of the three sendCommand methods are valid and it doesn’t have any other information to go on to tell it which one is the one it should call. Both are equally valid. So it generates an error; a different error from here that says something like “Ambiguous method call blah blah blah”.

This is why I recommend never casting to DecimalType in Rules and always casting to Number. In your Rule you want to treat the State as a Number anyway and it avoids this specific error.

But, like I said, that error is not what OP experienced above. Why there was an error and why just casting to Number instead of DecimalType fixed it.

I’ve been toying with the idea of a “just enough programming tutorial to write OH Rules” but it’s a really big problem. How much is just enough? How much is too much? Is it really appropriate to create and maintain as part of the OH docs themselves? Should I write a book?

I’ve been thinking about this and agree it’s a really big problem. Most non programmers search the site for rules, to copy and paste, without really understanding how the code works. This works for the most part but if adjustments are needed for extra functionality then a post for help usually results in a solution and everyone is happy.

So, the question is, how many people are willing to read a book and learn vs posting a problem and receiving a solution? You may be able to answer that question by looking at the number of post, for help with a rule, and the amount of info that is given by the poster. How many times have you seen a reply, to the OP, that ask to provide more info than just the error message? If there wasn’t a description of what to post when creating a new topic you could simply say they didn’t know any better.

Personally, I would love to see you make a tutorial or write a book about OH rules. Maybe start with a good foundation, what is an Object, Function, Class, etc… and how they work together. I’m not a programmer, so this may be similar to someone trying to do calculus or trig without learning algebra first. In this case your book may require a few volumes. :thinking:

On the other hand, if you do just enough, how often would you have to revise your tutorial. I’ve read several post about OH updates and rules not working afterwards. The solution often involves something to do with how the rule was coded. This also brings up the question, how many version of OH would a just enough tutorial cover?

I’ll stop my rambling here because I suspect you’ve already thought thru everything above, plus some.:grinning: My opinion would be to write the tutorial/book, start with a good foundation and understanding, that allows us to adapt to future changes and possibly even community contributions.

1 Like

These were exactly the issues I was frustrated with Xtend. Lets try Kotlin, I need to work hard. Kotlin does automatic type casting e.g String 7.7 to target Double 7.7.
Rich, don’t write your book yet man, wait for Kotlin support! :slight_smile:

I see you just replied to a post with: “Can you please paste full rule, or at least a stripped down version that reproduces your issue?”:smirk:

Xtend, Kotlin, JavaScript, Python, etc… doesn’t make a difference when your having to ask for info that should have been provided as part of a new topic. I can’t see an agenda, at lease collectively, for a tutorial or book helping when people are already doing less than minimum.

Yes, I want a book but I won’t be selfish. There are many others that would benefit but the big question remains. With all things considered, is the time and effort really worth it?

Yes, till we monopolize the automation market. :wink: Our rivals (in my opinion) : Samsung SmartThings, Home Assistant, Google Home, Amazon (must be planing big), Belkin Wemo, Nest Labs, Wink, Apple HomeKit and various “hubs” like Vera that are out there without any strong community support or open source code, but they may got money power, they can buy a lot of support staff.

Its a transitional phase, that experienced guys like Rich, you, @5iver have to spend their energy on little issues, we can cover most of that, hopefully, with modern languages like Scala/Kotlin. UI based rules, will cover, maybe basic stuff. If newbie’s are having issues and we don’t respond, we lose users, and eventually the project will be irrelevant.

But if we monopolize, the army of app developer community will help users with common+complex stuff, we just need to give devs a solid platform.

Bear in mind Microsoft had an army of support staff to help customers, to ensure their monopoly.

So when I say just enough programming I mean programming concepts. The sorts of things that exist in almost every programming language, presented with the Rules DSL as the exemplar.

From that perspective it would some never have to change because the concepts never change and for the most part they way they are implemented in the Rules DSL are not something that will change because they are fundamental to how the language works.

Three posts and changes you are referring to are caused by the parse getting a little more strict and providing better error reporting as a result (i.e. some rules with errors used to work but now the errors can’t be ignored) or they’re are changes to how Rules are defined which is not a general programming subject but something very specific to Rules DSL.

And notice I keep saying Rules DSL instead of Xtend. Rules DSL != Xtend. There is some overlap but they are not the same.

All of them. Anything more specific belongs in the Rules docs, not in a separate tutorial or book.

Even if Kotlin were available tomorrow, the Rules DSL would remain the default rules engine for a good long time. No book I wore would become OBE anything soon.

No but it sure saves us a hell of a lot of time to be able to respond with a link to an answer instead of rewriting the same answer over and over.

I’d like to be able to respond with a link to this for some of the parts for help that show a complete lack of knowledge of how programming works (variables, if statements, etc.) The same way I can respond with links to the Beginner’s Tutorial and concepts section of the docs when users misunderstand let OH concepts.

Don’t hold your breath. Even using Scratch isn’t going to help self proclaimed noon technical people understand how to program rules. One of the reasons I actually like the Rules DSL is because it purposefully removed whole features of the Xtend language to provide a simpler programming environment for new users.

Having access to JSR223 languages, Kotlin, Scala et Al it’s not going to do anything to help those users. Experimental Rules Engine and perhaps if Chris resurects the rule builder in Habmin will. All these more robust languages are great for those of us who are already developers but they don’t help the non-technical folks at all. In fact they raise the already high barrior to entry even higher for these types of users.

Helping theses users to “think like a programmer” would be my goal. The rest is just learning syntax and libraries. And the Rules DSL doesn’t have too many of the latter and the former isn’t really all that bad.

Adding Kotlin or any of the other languages (you might consider Processing as well) are great for OH developers and great for those who are already programmers or inclined to learn. There are IMHO a step backwards for supporting the users who have never programers before and who just want to turn on their fan when the temp goes above a threshold.

Exactly, I want to attract more of those, but first we need to have a “decent+modern” language, IDE support and tool set and integration with ESH core in place. Then those devs will do custom, region specific apps on those foundations. I never meant non-tech users to digest our scripting paradigms. UI rule engine should stretch itself on those lines.

YES, this would be very beneficial to the community, regardless of the language now or in the future.

Book title, “Thinking like a Programmer an OpenHab Foundation” ?:crossed_fingers:

1 Like

I agree with this thought. Every kid should be taut how to think like a programmer. Its gonna save species in long run when we become multi-planetary. There won’t be any community helping those on distant planets to fix their issues with spaceships or smart homes in those environments.

1 Like

I’m certain I wont be around to worry about multi-planetary issues.:joy: But I do agree with kids being taught to think like programmers. :exploding_head: More importantly I think all kids should serve their country, if only for 1 to 2 years . Learn to take orders, discipline, respect, pride, honor. Values IMHO that families now days lack. As a disabled vet and parent, I can say this with truth…somebody needs to get the belt out the closet or programming will be the least of future issues.

My last reply as this is getting off topic (and maybe a bit personal):roll_eyes:

Thanks to all.

1 Like

Thats a great thing you said. We humans are better than other species because we co-operate and collaborate. The concepts like nation, religion, tawn, city, family that we get attached to are mere manifestations of that core-dna. We like our buddies and we sacrifice our own life to save others. The alpha-males in (most) other mammal species don’t do that.
I think its about time we think like species rather than countries, skin colors, regions or religions.

Off topic!!

I am the Lorax. I speak for the trees, er, the non-technical users. They are my primary, first, and highest priority (on this forum at least). All of my responses and opinions expressed on this forum are from this perspective.

Not that I don’t think it’s important, but I could not care less about any changes or improvements to OH except in so far as they impact the users, in particular the non-technical users. These users consume the majority of my time so it is a position of self preservation.

This might help explain some of the postings I make. I’m very critical when it comes to changes that impacts the end users. I always ask “is this going to make it easier for the not-technical users?” If the answer is no I push back. If the answer is yes I help out as much as I can. If the answer is maybe, I need the proposer to prove to me how it makes things better for the non-technical users k or at least how it doesn’t make things worse.

HI all, thanks much for this discussion, very interesting.
Just a preamble: I am certainly a tech enthusiast, have limited programming background (stopped around the point when Turbo Pascal incorporated object-oriented programming)…and love home automation when it just does things…i.e., not in my face, it simply works, as one would expect it for any given situation. Now, the last is the tallest of all orders of course. In the context of this discussion, I would just add the following remarks from a user perspective.

Maybe my biggest problems in following along and learning a new skill is a combination of overhead and short-hand writing style. Keep in mind that my day job has nothing to do with computers more than email and MS-Office…and I am not every night fiddling with OH or even thinking about it. Just to make it clear, I understand that somethings are simply difficult, no matter what, and I will not even start talking about date/time issues; while very relevant to home automation, I understand why this has a mind-boggling complexity.

A little more down to earth, for example, the syntax of timers, or lambdas is still a mystery to me…what exactly needs to be before the “|” and what after and why? and do I need to declare a new variable for this, is it a function, etc? And this is despite the efforts of many to explain this to newbies over and over on this forum…I read many of these posts…but, for me there is nothing that makes much sense.

Reading up on kotlin a little (thanks @diyha) intrigued me immediately as it makes intuitively more sense to me than say, Java. Reading design patterns (thanks @rlkoshak, you just will be my hero forever) are critical, as abstract concepts come to life.

At a higher level, I understand that OHs DSL is here to stay for a while…so be it. But at least it will not and does not help me a lot. It is easy to write simply rules in it (but then simple is always easy), but it gets quite more complicated quickly; and the only real source is the forum…but try as I might, I am still missing posts that are relevant to my problems, just because, I am traveling, cannot read every day, etc.

Sometimes I feel, that the biggest hurdle and the biggest challenge is not to write a comprehensive description on how a different ecosystem works; it is translating how this makes sense. For me, one of the entrance hurdles is alway the amount of overhead, while it is boiler-plate and trivial…I am struggling a lot before I am recognizing it as such, and then cannot help the feeling of “why do I need to type this if it is all boilerplate and standard”, because at the end of the day, I still have to learn every line…

So, I guess what I want to say is: computer languages are always logic…duh…and always make perfect sense…double duh…but few are ever intuitive, or have just a too high barrier of entrance to read up on call definition boilerplate language, on syntax that is close to Xtend or Java (because it can use all the libraries), but then not because…what as if in my case, I don’t know Java or Xtend…then I have to read up on both.

This was not a rant: home automation is not easy and I will be forever grateful about the time that all the knowledgable people put in to help guys like me understand better…and there are many, many on this forum…really thank you so much. I learned so much from all of you, and I still have so much to learn.

1 Like

We are getting better, one step at a time, my friend. Thanks for sharing your heart.

Rich, you fight your battle, I’ll fight mine. :+1:
From my perspective, more devs means more life energy directed to this project. And devs can make a difference. Windows/Apples/Androids are not successful because “users” could program for it, its devs.
No offense, the Xtend and UI based rule engine are both pathetic (in my opinion), one targets devs and other users, they both fail at that. We can’t get hung up on those forever. We need to move on.

I have moved this conversation to a new topic.

https://community.openhab.org/t/making-oh-rules-easier-for-everyone/50177

Thanks all

1 Like