[SOLVED] Item linked with file

I have a file (let’s say ‘powerstatus’) that has only 2 values 0 or 1.
(This is just a script on another server that echo 0 or 1 into that file.)

Is there a way that I create an item ‘from’ this file?
Or what’s a good way?

The end goal is to update an item with the power status of servers. That I can use in rules, sitemaps…
And linked with this, send out messages, reboot/shutdown machines…

Not directly, but you could use the executeCommandLine action

for example:

var String result = executeCommandLine("cat path/to/file/file.txt", 1000)
if (result == "0") {
    MyItem.postUpdate(ON)
} else if (result == "1") {
    MyItem.postUpdate(OFF)
)
1 Like

Works great!!! Thanks!

Hereby the convert rule, and the rule that’s been triggered by it:

rule "check powerstatus of Hypervisor"
//Convert textvalue 0 or 1 towards OH item ON or OFF.
when
	Time cron "0 0/1 * * * ?"
then
	Thread::sleep(10000) // Give other server some time
	var String result = executeCommandLine("cat /diy/power_status",2000)
	if (result == "0") {
    		HypervisorPower.postUpdate(OFF)
		} 
	else if (result == "1") {
    		HypervisorPower.postUpdate(ON)
	}
end


rule "Action for Hypervisor on power failure"
//Trigger action if power is lost
when
	Item HypervisorPower changed from ON to OFF
then
	<knip>
end




ps The thread of 10 sec is because the other server is also trigger every minute. This way, I’m sure that the other server is finished with his check. Else I would lose a whole minute, and with my small UPS… :wink:

So why not trigger the rule using `10 0/1 * * * ?" ? This is functionally equivalent to what you are doing only you won’t be tying up a runtime thread waiting for those ten seconds.

2 Likes

I come from the linux world. In cron, no seconds, only minutes… :open_mouth:
So not used to think that way. :wink:

Thanks for the remark! I’ve updated it accordingly…

rule "check powerstatus of Audrey"                                                                                                                                                                                                                       
when                                                                                                                                                                                                                                                     
        Time cron "10 0/1 * * * ?"                                                                                                                                                                                                                       
then  
.....