Jump to content




CCSensors Information about a IC2 EU Storage Unit


17 replies to this topic

#1 lysdal

  • New Members
  • 12 posts

Posted 04 August 2012 - 08:08 PM

We've tried everything now. So im gonna try asking here.
We are trying to make a CCSensor that will give information to a monitor about a EU Storage Unit (Batbox)
It gives a error at the
write("Energy : "..Energy.."/"..Max.." ")
line
term.clear()
term.setCursorPos(1,1)
sensor = sensors.getReadingAsTable("right", "EUSensor","maxStorage" ,"energy" ,"tier")
Max = sensor[1]
Energy = sensor[2]
Tier = sensor[3]
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time		  : "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy		: "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier		  : "..unit.." ")
end
I just cant get it to work :P/>

#2 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 04 August 2012 - 08:22 PM

Maybe you have a nil Value in Energy or Max. Check that. Or give us the error Message :P/>
And, if I remember correctly, dont you need to index the dictionary table returned by getReadingAsTable(...) ?
But I am not so sure about that, just check the Variable contents.

#3 lysdal

  • New Members
  • 12 posts

Posted 04 August 2012 - 08:26 PM

The error is
gui:29: attempt to concatenate nil and string
How would i index the table returned?

#4 lysdal

  • New Members
  • 12 posts

Posted 04 August 2012 - 08:42 PM

I need help D:

#5 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 04 August 2012 - 11:45 PM

Ok. That is what I expected.

local Reading = sensors.getSensorReadingAsDict(<SIDE>,<SENSOR>,<TARGET>,<PROBE>);


should give you a Dictionary of all Sensor Readings.

You may then get every reading printed, by looping through :

for Name, Value in pairs(Reading) do
  print(Name, Value)
end

EDIT :

If you just want e.g. "tier", then you acces it like that :

local Tier = Reading.tier
-- OR
local Tier = Reading.["tier"]

But you need to be sure, the Key to the Value is called "tier".

I just made a test.

You need to get all Targets first!

In your case it should be:
Targets = sensors.getAvailableTargetsforProbe("right", <NAME of your sensor (EUSensor?)>, "EUStorage")

if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", <NAME of your sensor (EUSensor?)>, Target[1], "EUStorage")

  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end


#6 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 08:11 AM

Could you give me the complete code? :P/>
because
term.clear()
term.setCursorPos(1,1)
Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", "EUSensor", Target[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time	: "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy	 : "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier	: "..unit.." ")
end
Dosent work
It gives the error
gui:7: attempt to index ? (a nil value)


#7 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 12:36 PM

bump

#8 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 05 August 2012 - 01:52 PM

Oh, Spelling ERROR! Sorry...

Target[1] -> Targets[1]

Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", "EUSensor", Targets[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end


#9 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 02:03 PM

Error D:
gui:36: attempt to concatenate nil and string
thats the
write("Energy	 : "..Energy.."/"..Max.." ")
line.
So it still have a problem reading the EU Storage Unit D:

#10 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 03:26 PM

bump

#11 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 05 August 2012 - 04:44 PM

Ok, then go debugging by yourself. Try finding the error by doing stuff like
print(Energy)
print(Max)
If you dont get any Values, it means the Readings Table is Empty. Because the Indexes are correct, I checked that twice.
Try adding this after my If-Clause:
for k, v in pairs(Reading) do
  print(k, v)
end
So we can get all readings printed out. I think the error is somewhere else, maybe the Hardware setup? There can only be a nil error, when there is a valid storage found, because of the if-clause. So, maybe, we got the wrong Probe? Just give me the prints please.

#12 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 05:18 PM

term.setCursorPos(1,5)
print(Energy)
term.setCursorPos(1,6)
print(Max)
Prints
=
But!
for k, v in pairs(Reading) do
  print(k, v)
end
Outputs some information
But not in the way we want it
It outputs Name position and type not energy or anything.
1name
2ic.common.TileEntityElectricBatBox@1986a8
3type
4TileEntityElectricBatBox
5xCoord
6323
7yCoord
868
9zCoord
1077


#13 lysdal

  • New Members
  • 12 posts

Posted 05 August 2012 - 07:29 PM

bump

#14 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 05 August 2012 - 08:15 PM

That explains a lot... I'm not very familiar with ccSensors, but this print looks like the print of the Target-Information table! And that means :

We used an erroneous Probe Name!

Finally, this seems to be the answer. I made a test Setup, and your Program works! :P/> Change line :
Reading = sensors.getReadingAsTable("right", "EUSensor", Target[1], "EUStorage")
To :
Reading = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[1], "EUStorage")

We never got the actual Reading, because we always got the target info... D: But this works!

My complete tested Code:
term.clear()
term.setCursorPos(1,1)
Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier

  for k, v in pairs(Reading) do print(k, V) end
else
  Max = 0
  Energy = 0
  Tier = 1
end
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time	 : "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy	: "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier	 : "..unit.." ")
end


#15 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 05 August 2012 - 08:20 PM

Proof of work at bottom.

I hope this helped, even if it was kind of a long wait :P/>
If you are not comfortable with this solution, I could help you code a more error-safe version.
Just PM me, because now I'm into ccSensors, i got the ropes.

Attached Thumbnails

  • Attached Image: Itworks.jpg


#16 Neywiny

  • Members
  • 36 posts
  • LocationUSA, east coast

Posted 05 July 2013 - 10:38 AM

Might be a bit late here, but the code didn't work. I tried it and it said (programnamehere):33: attempt to concatenate nil and string.
The time still works, and the utilities and storage unit test is still there, but it will not show the energy.

#17 zane20062

  • New Members
  • 2 posts

Posted 19 December 2013 - 09:20 PM

Im trying the same thing! Your codes great but it just wont show the energy!
I tinkered around abit and i got no errors but it didnt print anything.
I also got nill value errors!
Someone please help!

#18 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 December 2013 - 10:56 PM

Please post your own topic with the code you're using and the mods you're using, including version numbers. This topic is ancient.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users